Sfoglia il codice sorgente

修改错误密码后锁定时间

baolei 2 mesi fa
parent
commit
1d1554aeb0

+ 1 - 0
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/constant/ShiroConstants.java

@@ -71,6 +71,7 @@ public class ShiroConstants
      * 登录记录缓存
      */
     public static final String LOGIN_RECORD_CACHE = "loginRecordCache";
+    public static final String LOGINTIME_RECORD_CACHE = "loginTimeRecordCache";
 
     /**
      * 系统活跃用户缓存

+ 24 - 6
08.src/Xingxi/xingxi-framework/src/main/java/com/xingxi/framework/shiro/service/SysPasswordService.java

@@ -1,12 +1,18 @@
 package com.xingxi.framework.shiro.service;
 
+import java.util.Date;
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import javax.annotation.PostConstruct;
 
 import com.xingxi.common.core.redis.RedisCache;
+import com.xingxi.common.utils.CacheUtils;
+import com.xingxi.common.utils.DateUtils;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheManager;
 import org.apache.shiro.crypto.hash.Md5Hash;
+import org.crazycake.shiro.RedisCacheManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
@@ -32,40 +38,51 @@ public class SysPasswordService
     @Autowired
     private RedisCache redisCache;
 
-    private Cache<String, AtomicInteger> loginRecordCache;
+    private HashMap<String, AtomicInteger> loginRecordCache;
+    private HashMap<String, Date> loginTimeRecordCache;
+
+//    private Cache<String, AtomicInteger> loginRecordCache;
 
     @Value(value = "${user.password.maxRetryCount}")
     private String maxRetryCount;
+    @Autowired
+    private RedisCacheManager cacheManager;
 
     @PostConstruct
     public void init()
     {
-//        loginRecordCache = cacheManager.getCache(ShiroConstants.LOGIN_RECORD_CACHE);
+
+        redisCache.setCacheObject(ShiroConstants.LOGIN_RECORD_CACHE, new HashMap<String, AtomicInteger>());
+        redisCache.setCacheObject(ShiroConstants.LOGINTIME_RECORD_CACHE, new HashMap<String, AtomicInteger>());
         loginRecordCache = redisCache.getCacheObject(ShiroConstants.LOGIN_RECORD_CACHE);
-        redisCache.expire(ShiroConstants.LOGIN_RECORD_CACHE, 60 * 5);
+        loginTimeRecordCache = redisCache.getCacheObject(ShiroConstants.LOGIN_RECORD_CACHE);
     }
 
     public void validate(SysUser user, String password)
     {
         String loginName = user.getLoginName();
 
-        AtomicInteger retryCount = loginRecordCache.get(loginName);
+        AtomicInteger retryCount = (AtomicInteger)loginRecordCache.get(loginName);
 
         if (retryCount == null)
         {
             retryCount = new AtomicInteger(0);
             loginRecordCache.put(loginName, retryCount);
+            loginTimeRecordCache.put(loginName, new Date());
         }
         if (retryCount.incrementAndGet() > Integer.valueOf(maxRetryCount).intValue())
         {
-            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount)));
-            throw new UserPasswordRetryLimitExceedException(Integer.valueOf(maxRetryCount).intValue());
+            if (DateUtils.getNowDate().getTime() - loginTimeRecordCache.get(loginName).getTime() < 1000 * 60 * 10) {
+                AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount)));
+                throw new UserPasswordRetryLimitExceedException(Integer.valueOf(maxRetryCount).intValue());
+            }
         }
 
         if (!matches(user, password))
         {
             AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", retryCount)));
             loginRecordCache.put(loginName, retryCount);
+            loginTimeRecordCache.put(loginName, new Date());
             throw new UserPasswordNotMatchException();
         }
         else
@@ -82,6 +99,7 @@ public class SysPasswordService
     public void clearLoginRecordCache(String loginName)
     {
         loginRecordCache.remove(loginName);
+        loginTimeRecordCache.remove(loginName);
     }
 
     public String encryptPassword(String loginName, String password, String salt)