当前位置:网站首页>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();}}
边栏推荐
猜你喜欢
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
Harmony OS Date ano UI 】 【 】 the basic operation
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
2022/08/02 Study Notes (day22) Multithreading
探索性测试的概念及方法
RequestContextHolder
GIS数据漫谈(五)— 地理坐标系统
t conditional judgment statement and if loop
Interface test practice | Detailed explanation of the difference between GET / POST requests
随机推荐
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
【 Harmony OS 】 【 ano UI 】 lightweight data storage
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
[Harmony OS] [ARK UI] ETS context basic operations
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
typescript45-接口之间的兼容性
MCM box model modeling method and source analysis of atmospheric O3
2022 Henan Mengxin League Game (4): Zhengzhou University of Light Industry E - Sleep Well
数字化时代,企业如何建立自身的云平台与商业模式的选择?
在竞争白热化的电商行业,链动2+1为什么还有企业在用
[Harmony OS] [ArkUI] ets development graphics and animation drawing
表的创建、修改与删除
3.张量运算
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
t条件判断语句与if循环
Concepts and Methods of Exploratory Testing
索引创建、删除与使用
超好用的画图工具推荐
GIS数据漫谈(六)— 投影坐标系统