当前位置:网站首页>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();}} 边栏推荐
猜你喜欢

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

redis键值出现 xacxedx00x05tx00&的解决方法

StarRocks July Community Update

typescript47-函数之间的类型兼容性
![[Harmony OS] [ArkUI] ets development graphics and animation drawing](/img/36/f4c91f794b1321f11a24505d1617fb.png)
[Harmony OS] [ArkUI] ets development graphics and animation drawing

【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability

typescript49-交叉类型

c语言结构体中的冒泡排序

typescript46-函数之间的类型兼容性

Concepts and Methods of Exploratory Testing
随机推荐
Test drive: project management module - curd development project
Interface testing framework combat (3) | JSON request and response assertion
Super handy drawing tool is recommended
2.何为张量
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
链动2+1模式简单,奖励结构丰厚,自主裂变?
DFS对剪枝的补充
【Harmony OS】【ARK UI】轻量级数据存储
数字化时代,企业如何建立自身的云平台与商业模式的选择?
typescript49-交叉类型
Flink状态
[Fine talk] Using native js to implement todolist
接口测试框架实战(三)| JSON 请求与响应断言
【Harmony OS】【ARK UI】Date 基本操作
MySQL 出现 The table is full 的解决方法
Kotlin-Flow常用封装类:StateFlow的使用
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
[Harmony OS] [ARK UI] ETS context basic operations
接口测试框架实战(一) | Requests 与接口请求构造
【Harmony OS】【FAQ】鸿蒙问题合集1