Set the tomcat virtual path, delete the specified file, and tomcat path

Source: Internet
Author: User

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>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.