当前位置:网站首页>用户密码验证
用户密码验证
2022-08-03 04:35:00 【Dzooooone_】
密码的验证-锁定
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.google.common.collect.Maps;
import com.inesa.basic.business.server.service.BasicUserInfoService;
import com.inesa.business.model.AuthorityResponse;
import com.inesa.business.model.BasicUserInfoVo;
@Component
public class AuthorityTools {
@Resource
private BasicUserInfoService userService;
/**
* 允许的最大登录次数
*/
@Value("${authority.retry.limit:3}")
private int retryLimit;
/**
* 用户锁定时间
*/
@Value("#{${authority.retry.lock:5} * 60 * 1000L}")
private long lockTime;
/**
* 用户登录锁定时间
*/
private Map<Long, Long> userLockTime = Maps.newConcurrentMap();
/**
* 用户登录失败次数
*/
private Map<Long, Integer> userLongTime = Maps.newConcurrentMap();
/**
* 对信息进行加密
*
* @param
* @return
*/
public String encoder(String password) {
return CryptoUtil.encrypt(password);
}
/**
* 验证是否正确
*
* @param username
* @param password 第几次登录
* @return
*/
public AuthorityResponse authority(String username, String password) {
BasicUserInfoVo user = userService.findByUserName(username);
// 验证用户是否存在
if (user == null) {
return AuthorityResponse.builder().check(false).error("用户不存在").build();
}
// 判断用户是否锁定
if (userLockTime.containsKey(user.getId())) {
if (System.currentTimeMillis() - userLockTime.get(user.getId()) < lockTime) {
if (userLongTime.getOrDefault(user.getId(), 0) > retryLimit) {
int minutes = (int) ((System.currentTimeMillis() - userLockTime.get(user.getId())) / 60000L);
return AuthorityResponse.builder().check(false)
.error(String.format("用户已锁定,请在%s分钟后重试", lockTime / 60000L - minutes)).build();
}
} else {
// 如果上次登录失败的时间已经超过时间限制,重置登录失败的次数
userLockTime.remove(user.getId());
userLongTime.remove(user.getId());
}
}
if (user.getState() == BasicUserInfoVo.DISABLE) {
return AuthorityResponse.builder().check(false).error("用户已被禁用").build();
}
if (user.getState() == BasicUserInfoVo.LOCKED) {
return AuthorityResponse.builder().check(false).error("用户已被锁定").build();
}
// 验证密码是否不正确
if (!password.equals(user.getPassword())) {
// 登录失败后更新登录失败的次数,以及更新登录失败的时间
int retriedTimes = userLongTime.getOrDefault(user.getId(), 0);
retriedTimes++;
userLongTime.put(user.getId(), retriedTimes);
userLockTime.put(user.getId(), System.currentTimeMillis());
return AuthorityResponse.builder().check(false)
.error("密码错误").build();
}
return AuthorityResponse.builder().userId(user.getId()).check(true).build();
}
}
边栏推荐
- 自组织是管理者和成员的双向奔赴
- Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?
- 常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- 【精讲】利用原生js实现todolist
- 如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
- OpenFOAM extracts equivalency and calculates area
- 13.机器学习基础:数据预处理与特征工程
- 11.机器学习基础:机器学习的四个分支
- Problems that need to be solved for interrupting the system
猜你喜欢
随机推荐
超好用的画图工具推荐
online test paper concept
汇编书摘抄
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
MySQL 入门:Case 语句很好用
Shell条件语句判断
Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?
工程水文学试题库
工程制图第九章作业
2.何为张量
Where is the value of testers
计组错题集
9.新闻分类:多分类问题
链动2+1模式简单,奖励结构丰厚,自主裂变?
4.深度学习的几何解释与梯度的优化
c语言结构体中的冒泡排序
Shell之条件语句
2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
How many moments have you experienced the collapse of electronic engineers?
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?