当前位置:网站首页>User password verification
User password verification
2022-08-03 04:43:00 【Dzooooone_】
Password verification-lock
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;@Componentpublic class AuthorityTools {@Resourceprivate BasicUserInfoService userService;/*** The maximum number of logins allowed*/@Value("${authority.retry.limit:3}")private int retryLimit;/*** User lockout time*/@Value("#{${authority.retry.lock:5} * 60 * 1000L}")private long lockTime;/*** User login lockout time*/private Map userLockTime = Maps.newConcurrentMap();/*** The number of user login failures*/private Map userLongTime = Maps.newConcurrentMap();/*** Encrypt information** @param* @return*/public String encoder(String password) {return CryptoUtil.encrypt(password);}/*** Verify that it is correct** @param username* @param password Number of times to log in* @return*/public AuthorityResponse authority(String username, String password) {BasicUserInfoVo user = userService.findByUserName(username);// Verify user existsif (user == null) {return AuthorityResponse.builder().check(false).error("User does not exist").build();}// Determine if the user is lockedif (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("User is locked, please try again in %s minutes", lockTime / 60000L - minutes)).build();}} else {// If the time of the last login failure has exceeded the time limit, reset the number of failed loginsuserLockTime.remove(user.getId());userLongTime.remove(user.getId());}}if (user.getState() == BasicUserInfoVo.DISABLE) {return AuthorityResponse.builder().check(false).error("User has been disabled").build();}if (user.getState() == BasicUserInfoVo.LOCKED) {return AuthorityResponse.builder().check(false).error("User has been locked out").build();}// Verify that the password is incorrectif (!password.equals(user.getPassword())) {// Update the number of failed logins after login failures, and update the time of failed loginsint retriedTimes = userLongTime.getOrDefault(user.getId(), 0);retrieveTimes++;userLongTime.put(user.getId(), retriedTimes);userLockTime.put(user.getId(), System.currentTimeMillis());return AuthorityResponse.builder().check(false).error("Password error").build();}return AuthorityResponse.builder().userId(user.getId()).check(true).build();}}
边栏推荐
猜你喜欢
随机推荐
Where is the value of testers
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
User password encryption tool
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
接口测试框架实战(二)| 接口请求断言
2022 Henan Mengxin League Game (4): Zhengzhou University of Light Industry E - Sleep Well
typescript46-函数之间的类型兼容性
MySQL 出现 The table is full 的解决方法
Practical application of WebSocket
MCM box model modeling method and source analysis of atmospheric O3
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
接口管理工具YApi怎么用?颜值高、易管理、超好用
在线密码生成工具推荐
CobalStrike(CS)基础超级详细版
C#异步和多线程
好消息!北京、珠海PMP考试时间来啦
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
12.机器学习基础:评估机器学习模型
Test drive: project management module - curd development project