懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.jcdm.framework.web.service;
 
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import com.jcdm.common.constant.CacheConstants;
import com.jcdm.common.constant.Constants;
import com.jcdm.common.core.domain.entity.SysUser;
import com.jcdm.common.core.redis.RedisCache;
import com.jcdm.common.exception.user.UserPasswordNotMatchException;
import com.jcdm.common.exception.user.UserPasswordRetryLimitExceedException;
import com.jcdm.common.utils.MessageUtils;
import com.jcdm.common.utils.SecurityUtils;
import com.jcdm.framework.manager.AsyncManager;
import com.jcdm.framework.manager.factory.AsyncFactory;
import com.jcdm.framework.security.context.AuthenticationContextHolder;
 
/**
 * 登录密码方法
 * 
 * @author jc
 */
@Component
public class SysPasswordService
{
    @Autowired
    private RedisCache redisCache;
 
    @Value(value = "${user.password.maxRetryCount}")
    private int maxRetryCount;
 
    @Value(value = "${user.password.lockTime}")
    private int lockTime;
 
    /**
     * 登录账户密码错误次数缓存键名
     * 
     * @param username 用户名
     * @return 缓存键key
     */
    private String getCacheKey(String username)
    {
        return CacheConstants.PWD_ERR_CNT_KEY + username;
    }
 
    public void validate(SysUser user)
    {
        Authentication usernamePasswordAuthenticationToken = AuthenticationContextHolder.getContext();
        String username = usernamePasswordAuthenticationToken.getName();
        String password = usernamePasswordAuthenticationToken.getCredentials().toString();
 
        Integer retryCount = redisCache.getCacheObject(getCacheKey(username));
 
        if (retryCount == null)
        {
            retryCount = 0;
        }
 
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
        {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount, lockTime)));
            throw new UserPasswordRetryLimitExceedException(maxRetryCount, lockTime);
        }
 
        if (!matches(user, password))
        {
            retryCount = retryCount + 1;
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.retry.limit.count", retryCount)));
            redisCache.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
            throw new UserPasswordNotMatchException();
        }
        else
        {
            clearLoginRecordCache(username);
        }
    }
 
    public boolean matches(SysUser user, String rawPassword)
    {
        return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
    }
 
    public void clearLoginRecordCache(String loginName)
    {
        if (redisCache.hasKey(getCacheKey(loginName)))
        {
            redisCache.deleteObject(getCacheKey(loginName));
        }
    }
}