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]);
|
}
|
}
|
}
|
|
}
|
}
|