Set the tomcat virtual path, delete the specified file, and tomcat path
I. background
The server is often hacked. An unknown malicious war file is often added to webapps. Response:
1. Modify the tomcat virtual path.
2. Check files in the webapps and work folders regularly and delete unknown files.
Ii. tomcat server. xml
Create the webApp and webWork folders under drive C, and modify the host node in server. xml:
<Host name="localhost" appBase="C:/webApp/" workDir="C:/webWork/" unpackWARs="true" autoDeploy="true">
3. Create a listener.
1. ProtectTaskListener. java
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;/*** file detection listener ** @ author liyulin lyl010991@126.com * @ version 1.0 2015-04-13 */public class ProtectTaskListener implements ServletContextListener {private Timer timer; public void contextInitialized (partial 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
Package com. px. listener; import java. io. file;/*** Delete webApp and webWork (except hospital1, installer, jswork, reports) other files ** @ author liyulin lyl010991@126.com * @ version 1.0 2015-04-13 */public class ProtectUtil {public static final String web1__path = "C:/webApp "; public static final String WORK_PATH = "C:/webWork"; public static final String [] WORK_FILE_PATH = new String [] {"hospital1", "installer", "js Work "," reports "}; public static void deleteOtherFile () {deleteWebAppsOtherFile (); deleteWorkOtherFile ();} /*** delete all files except hospital1 in webapps */public static void deleteWebAppsOtherFile () {File file = new File (website_path); if (file. exists () {File [] fileLists = file. listFiles (); for (File f: fileLists) {if (! F. getName (). equals ("hospital1") {delfileorderecloud (f );}}}} /*** delete all files except "hospital1", "installer", "jswork", and "reports" under work */public static void deleteWorkOtherFile () {File file = new File (WORK_PATH); if (file. exists () {File [] fileLists = file. listFiles (); for (File f: fileLists) {boolean isDelete = true; // delete a File flag. True: delete for (String workFile: WORK_FILE_PATH) {if (f. getName (). equals (workFile) {isDelete = false; break ;}}if (isDelete) {delfileorderecloud (f );}}}} /*** delete folders and files ** @ param file: files to be deleted, folders */private static void delfileorderecloud (File file) {if (file. exists () {if (file. isDirectory () {File [] files = file. listFiles (); for (File subFile: files) {delfileorderecloud (subFile);} file. delete ();} else {file. delete ();}}}}
4. modify web. xml
Add the following under the web-app node:
<listener> <listener-class> com.px.listener.ProtectTaskListener </listener-class> </listener>