|
@@ -1,8 +1,5 @@
|
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-import com.ruoyi.common.exception.user.UserException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.core.Authentication;
|
|
@@ -12,13 +9,14 @@ import com.ruoyi.common.constant.Constants;
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
|
-import com.ruoyi.common.exception.user.UserPasswordRetryLimitExceedException;
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
|
import com.ruoyi.framework.security.context.AuthenticationContextHolder;
|
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
/**
|
|
|
* 登录密码方法
|
|
|
*
|
|
@@ -38,7 +36,7 @@ public class SysPasswordService
|
|
|
|
|
|
/**
|
|
|
* 登录账户密码错误次数缓存键名
|
|
|
- *
|
|
|
+ *
|
|
|
* @param username 用户名
|
|
|
* @return 缓存键key
|
|
|
*/
|
|
@@ -56,6 +54,14 @@ public class SysPasswordService
|
|
|
return retryCount >= Integer.valueOf(maxRetryCount).intValue();
|
|
|
}
|
|
|
|
|
|
+ public String retryMessage(String username) {
|
|
|
+ Integer retryCount = redisCache.getCacheObject(getCacheKey(username));
|
|
|
+ return MessageUtils.message("user.password.retry.limit.count", retryCount);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String NEED_VERIFICATION_CODE = "NEED_VERIFICATION_CODE";
|
|
|
+
|
|
|
public void validate(SysUser user)
|
|
|
{
|
|
|
Authentication usernamePasswordAuthenticationToken = AuthenticationContextHolder.getContext();
|
|
@@ -82,7 +88,10 @@ public class SysPasswordService
|
|
|
retryCount = retryCount + 1;
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
|
|
|
MessageUtils.message("user.password.retry.limit.count", retryCount)));
|
|
|
- redisCache.setCacheObject(getCacheKey(username), retryCount);
|
|
|
+ redisCache.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
|
|
|
+ if (retryCount >= Integer.valueOf(maxRetryCount).intValue()) {
|
|
|
+ throw new RuntimeException(NEED_VERIFICATION_CODE);
|
|
|
+ }
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
}
|
|
|
else
|
|
@@ -91,6 +100,8 @@ public class SysPasswordService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
public boolean matches(SysUser user, String rawPassword)
|
|
|
{
|
|
|
return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
|