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