Browse Source

feat: login error lock 10m

shc 11 months ago
parent
commit
1dae34c3e9

+ 2 - 2
hnqz-auth/src/main/java/com/qunzhixinxi/hnqz/auth/handler/HnqzAuthenticationFailureEventHandler.java

@@ -65,14 +65,14 @@ public class HnqzAuthenticationFailureEventHandler implements AuthenticationFail
         final String errorKey = "login:error:lock:" + username;
         final String lockKey = "login:error:limit:times:" + username;
 
-        if (Boolean.TRUE.equals(redisTemplate.hasKey("lockKey"))) {
+        if (Boolean.TRUE.equals(redisTemplate.hasKey(lockKey))) {
             Object o = redisTemplate.opsForValue().get(lockKey);
             int limit = Integer.parseInt(o.toString());
 
             if (limit >= 5) {
                 redisTemplate.opsForValue().set(errorKey, username, 10, TimeUnit.MINUTES);
             } else {
-                redisTemplate.opsForValue().increment(lockKey, 1);
+                redisTemplate.opsForValue().set(lockKey, limit + 1);
             }
 
 

+ 11 - 6
hnqz-auth/src/main/java/com/qunzhixinxi/hnqz/auth/service/HnqzUserDetailsServiceImpl.java

@@ -23,6 +23,7 @@ import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.stereotype.Service;
 
+import javax.security.auth.login.AccountLockedException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
@@ -67,14 +68,14 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
 
 
 
-        Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
-        if (cache != null && cache.get(rawUsername) != null) {
-            return (HnqzUser) cache.get(rawUsername).get();
-        }
+        // Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
+        // if (cache != null && cache.get(rawUsername) != null) {
+        //     return (HnqzUser) cache.get(rawUsername).get();
+        // }
 
         R<UserInfo> result = remoteUserService.info1(rawUsername, isApp, SecurityConstants.FROM_IN);
         UserDetails userDetails = getUserDetails(result);
-        cache.put(username, userDetails);
+        // cache.put(username, userDetails);
         return userDetails;
     }
 
@@ -117,6 +118,10 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
         boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
         // 构造security用户
 
+        if (!CommonConstants.STATUS_NORMAL.equals(user.getLockFlag())){
+            throw new RuntimeException("当前账号已经锁定了");
+        }
+
         return new HnqzUser(user.getUserId(),
                 user.getDeptId(),
                 user.getPhone(),
@@ -127,7 +132,7 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
                 enabled,
                 true,
                 true,
-                !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()),
+                CommonConstants.STATUS_NORMAL.equals(user.getLockFlag()),
                 authorities,
                 user.getPlatId(),
                 user.getDrugEntId(),