Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Windows教程綜合 >> 系統常見問題解答 >> 設置tomcat虛擬路徑,刪除指定文件

設置tomcat虛擬路徑,刪除指定文件

日期:2017/1/20 17:45:56      編輯:系統常見問題解答

一、背景

服務器經常被人黑。webapps下時常莫名其妙的多了一個未知的惡意war文件。對此的反應:

1、修改tomcat虛擬路徑。

2、定時檢測webapps和work文件夾下的文件,刪除不知名的文件。

二、tomcat的server.xml

在C盤下新建webApp和、webWork兩個文件夾,將server.xml中的host節點修改為:

 

? 1 <host appbase="C:/webApp/" autodeploy="true" name="localhost" unpackwars="true" workdir="C:/webWork/"></host> 三、新建一個listener。

 

1、ProtectTaskListener.java

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.px.listener;   import com.px.util.Config; import java.util.Timer; import java.util.TimerTask; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;   /**  * 文件檢測監聽器  *  * @author liyulin [email protected]  * @version 1.0 2015-04-13  */ public class ProtectTaskListener implements ServletContextListener {     private Timer timer;       public void contextInitialized(ServletContextEvent event) {         timer = new Timer();         timer.schedule(                 new TimerTask() {                     public void run() {                         ProtectUtil.deleteOtherFile();                     }                 }, Config.CHECK_TIME);     }       public void contextDestroyed(ServletContextEvent event) {         timer.cancel();     }   }

 

2、ProtectUtil.java

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package com.px.listener;   import java.io.File;   /**  * 刪除webApp和webWork下的(除了hospital1、installer、jswork、reports)其它文件  *  * @author liyulin [email protected]  * @version 1.0 2015-04-13  */ public class ProtectUtil {       public static final String WEBAPPS_PATH = "C:/webApp";     public static final String WORK_PATH = "C:/webWork";     public static final String[] WORK_FILE_PATH = new String[]{         "hospital1",         "installer",         "jswork",         "reports"     };       public static void deleteOtherFile() {         deleteWebAppsOtherFile();         deleteWorkOtherFile();     }       /**      * 刪除webapps下除了hospital1的其它所有文件      */     public static void deleteWebAppsOtherFile() {         File file = new File(WEBAPPS_PATH);         if (file.exists()) {             File[] fileLists = file.listFiles();             for (File f : fileLists) {                 if (!f.getName().equals("hospital1")) {                     delFileOrDerectory(f);                 }             }         }     }       /**      * 刪除work下除了"hospital1"、"installer"、"jswork"、"reports"的所有文件      */     public static void deleteWorkOtherFile() {         File file = new File(WORK_PATH);         if (file.exists()) {             File[] fileLists = file.listFiles();             for (File f : fileLists) {                 boolean isDelete = true;// 刪除文件標志。true:刪除                 for (String workFile : WORK_FILE_PATH) {                     if (f.getName().equals(workFile)) {                         isDelete = false;                         break;                     }                 }                 if (isDelete) {                     delFileOrDerectory(f);                 }             }         }     }       /**      * 刪除文件夾、文件      *      * @param file 待刪除文件、文件夾      */     private static void delFileOrDerectory(File file) {         if (file.exists()) {             if (file.isDirectory()) {                 File[] files = file.listFiles();                 for (File subFile : files) {                     delFileOrDerectory(subFile);                 }                 file.delete();             } else {                 file.delete();             }         }     } }
Copyright © Windows教程網 All Rights Reserved