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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.whyc.task;
 
import com.whyc.pojo.PageParam;
import com.whyc.service.PageParamService;
import com.whyc.service.UserService;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.List;
import java.util.TimerTask;
 
/**
 * 账号解锁:默认20分钟
 * 账号登录失败时间超过24小时,移除
 * TODO 尚未测试
 */
public class AccountLockStrategyTask extends TimerTask {
 
    private ServletContext servletContext;
 
    @Resource
    private UserService userService;
 
    @Resource
    private PageParamService pageParamService;
 
    public AccountLockStrategyTask(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
 
    @Override
    public void run() {
        //临时账号检测是否过期,如果过期,更改状态为注销
        userService.updateExpiredAccount();
 
        //查询账号解锁策略
        PageParam accountUnLock = (PageParam) ((List)pageParamService.findByCategoryId(9).getData()).get(1);
        if(accountUnLock.getStatus()==2){ //自动解锁策略
            //查询账号锁定时间,超过20分钟解锁
            userService.unLock();
        }
 
        //登录失败时间超过24小时记录,内存中application移除
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        //24小时前的时间戳
        long oneDayBefore = System.currentTimeMillis()-24*60*60*1000;
 
        while (attributeNames.hasMoreElements()){
            String attributeName = attributeNames.nextElement();
            if(attributeName.contains("_login_fail_times_")){
                if(oneDayBefore>Long.parseLong(attributeName.split("_login_fail_times_")[1])) {
                    servletContext.removeAttribute(attributeName);
                }
            }
        }
 
    }
}