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