当前位置:网站首页>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();}} 边栏推荐
猜你喜欢
随机推荐
Assembly answers
2.何为张量
自组织是管理者和成员的双向奔赴
1.一个神经网络示例
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
User password encryption tool
mysql bool盲注
Browser listens for tab closing
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
接口测试框架实战(四)| 搞定 Schema 断言
How to prepare for the test interface test data
Bubble sort in c language structure
typescript45-接口之间的兼容性
技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
接口测试框架实战(三)| JSON 请求与响应断言
Redis缓存雪崩、缓存穿透、缓存击穿
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计









