whycxzp
2022-03-17 24360b947a95337a052e20acfbbcf9c1a7e20b17
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
package com.whyc.task;
 
import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.TimerTask;
 
/**
 * 防重放功能随机码清除
 */
public class PreventTryTask extends TimerTask {
 
    private ServletContext servletContext;
 
    public PreventTryTask(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
 
    @Override
    public void run() {
 
        //60秒前的记录,内存中application移除
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        long oneMinuteBefore = System.currentTimeMillis()-70*1000;
 
        while (attributeNames.hasMoreElements()){
            String attributeName = attributeNames.nextElement();
            if(attributeName.contains("randomStr_")){
                if(oneMinuteBefore>Long.parseLong((String) servletContext.getAttribute(attributeName.split("randomStr_")[1]))) {
                    servletContext.removeAttribute(attributeName);
                    servletContext.removeAttribute(attributeName.split("randomStr_")[1]);
                }
            }
        }
 
    }
}