puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
95
96
97
98
99
100
101
102
package com.sinata.modular.system.controller.util;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sinata.common.enums.EnumRedisKey;
import com.sinata.common.model.AccountCheckNumVo;
import com.sinata.core.common.exception.InvalidKaptchaException;
import com.sinata.core.shiro.ShiroKit;
import com.sinata.core.util.DateUtils2;
import com.sinata.modular.system.model.User;
import com.sinata.modular.system.service.IUserService;
import com.sinata.modular.system.service.RedisTemplateService;
import org.apache.shiro.authc.CredentialsException;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
/**
 * 验证密码/验证码次数限制
 * @author goku
 * @date 2023/3/5
 */
@Service
public class AccountCheckUtil {
 
    @Resource
    private RedisTemplateService redisTemplateService;
 
    @Resource
    private IUserService userService;
 
    /**
     * 账号验证码-验证次数
     * @param account
     */
    public void accountCheckCodeNum(String account, boolean flag) {
        // 获取账户验证次数
        Object chMap = redisTemplateService.hashGet(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account);
        String today = DateUtils2.formatDate(new Date(), "yyyy-MM-dd");
        AccountCheckNumVo ch;
        if(chMap != null) {
            ch = JSON.parseObject(chMap.toString(), AccountCheckNumVo.class);
            if(today.equals(ch.getCheckCodeDate())) {
                ch.setCheckCodeNum(ch.getCheckCodeNum() + 1);
            } else {
                ch.setCheckCodeNum(1);
            }
        } else {
            ch = new AccountCheckNumVo();
            ch.setAccount(account);
        }
        ch.setCheckCodeDate(today);
 
        if(ch.getTotalCodeNum() < ch.getCheckCodeNum()){
            redisTemplateService.hashPushHashMap(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account, JSON.toJSONString(ch));
            throw new InvalidKaptchaException();
        } else {
            if (flag) {
                ch.setCheckCodeNum(1);
            }
            redisTemplateService.hashPushHashMap(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account, JSON.toJSONString(ch));
        }
    }
    /**
     * 账号密码-验证次数
     * @param account
     */
    public void accountCheckPwdNum(String account, String password) {
        // 获取账户验证次数
        Object chMap = redisTemplateService.hashGet(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account);
        String today = DateUtils2.formatDate(new Date(), "yyyy-MM-dd");
        AccountCheckNumVo ch;
        if(chMap != null) {
            ch = JSON.parseObject(chMap.toString(), AccountCheckNumVo.class);
            if(today.equals(ch.getCheckPwdDate())) {
                ch.setCheckPwdNum(ch.getCheckPwdNum() + 1);
            } else {
                ch.setCheckPwdNum(1);
            }
        } else {
            ch = new AccountCheckNumVo();
            ch.setAccount(account);
        }
        ch.setCheckPwdDate(today);
 
        if(ch.getTotalPwdNum() < ch.getCheckPwdNum()){
            redisTemplateService.hashPushHashMap(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account, JSON.toJSONString(ch));
            throw new CredentialsException();
        } else {
            List<User> userList = userService.selectList(new EntityWrapper<User>().eq("account", account).ne("status", 3));
            for (User u : userList) {
                String oldMd5 = ShiroKit.md5(password, u.getSalt());
                if (u.getPassword().equals(oldMd5)) {
                    ch.setCheckPwdNum(1);
                }
            }
            redisTemplateService.hashPushHashMap(EnumRedisKey.MAP_MEM_ADMIN_CHECK.index, account, JSON.toJSONString(ch));
        }
    }
}