kongzy
2023-12-08 ca5445257b1fdeceddf3fcc2dea18c442023aeb7
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
package com.gkhy.assess.framework.shiro.service;
 
import com.gkhy.assess.common.constant.CacheConstant;
import com.gkhy.assess.common.utils.JwtTokenUtil;
import com.gkhy.assess.common.utils.RedisUtils;
import com.gkhy.assess.system.domain.SysUser;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.atomic.AtomicInteger;
 
@Component
public class SysPasswordService {
 
    @Autowired
    private RedisUtils redisUtils;
 
 
    @Value(value = "${user.password.maxRetryCount:5}")
    private Integer maxRetryCount;
 
    public void validate(SysUser user, String password) throws AuthenticationException {
        String username=user.getUsername();
        String key= redisUtils.generateKey(CacheConstant.SYS_LOGIN_RECORD_CACHE+":"+username);
        Integer retryCount= (Integer) redisUtils.get(key);
        if(retryCount==null){
            retryCount=0;
        }
        ++retryCount;
        if(retryCount>maxRetryCount){
            throw new AuthenticationException("登录次数已达上限,5分钟之后再试");
        }
        if(!matches(user,password)){
            redisUtils.set(key,retryCount,60*5);//5分钟后释放
            throw new AuthenticationException("登录密码错误");
        }else{
            redisUtils.del(key);
        }
 
    }
 
    public boolean matches(SysUser sysUser,String newPassword){
        return sysUser.getPassword().equals(JwtTokenUtil.encryptPassword(sysUser.getUsername(),newPassword,sysUser.getSalt()));
    }
 
 
 
}