|
@@ -76,6 +76,8 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -204,6 +206,7 @@ public class LoginController extends BaseController{
|
|
|
message = "您的登录信息过期,请重新登录!";
|
|
|
}
|
|
|
|
|
|
+
|
|
|
model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, username);
|
|
|
model.addAttribute(FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM, rememberMe);
|
|
|
model.addAttribute(FormAuthenticationFilter.DEFAULT_MOBILE_PARAM, mobile);
|
|
@@ -213,16 +216,32 @@ public class LoginController extends BaseController{
|
|
|
if (logger.isDebugEnabled()){
|
|
|
//logger.debug("login fail, active session size: {}, message: {}, exception: {}",sessionDAO.getActiveSessions(false).size(), message, exception);
|
|
|
}
|
|
|
+ User user = UserUtils.getByLoginName(username);
|
|
|
+ if (user != null){
|
|
|
+ // 非授权异常,登录失败,验证码加1。
|
|
|
+ if (!UnauthorizedException.class.getName().equals(exception)){
|
|
|
+ model.addAttribute("isValidateCodeLogin", isValidateCodeLogin(user.getLoginName(), true, false));
|
|
|
+ }
|
|
|
|
|
|
- // 非授权异常,登录失败,验证码加1。
|
|
|
- if (!UnauthorizedException.class.getName().equals(exception)){
|
|
|
- model.addAttribute("isValidateCodeLogin", isValidateCodeLogin(username, true, false));
|
|
|
+ // 验证失败清空验证码
|
|
|
+ //request.getSession().setAttribute(ValidateCodeServlet.VALIDATE_CODE, IdGen.uuid());
|
|
|
+ jedis = JedisUtils.getResource();
|
|
|
+ jedis.set(ValidateCodeServlet.VALIDATE_CODE, IdGen.uuid());
|
|
|
+
|
|
|
+ //从redis中获取失败次数
|
|
|
+ String key = "loginFailNum:"+user.getLoginName();
|
|
|
+ String loginFailNum = JedisUtils.getResource().get(key);
|
|
|
+ if (StringUtils.isNotBlank(loginFailNum)){
|
|
|
+ int num = Integer.parseInt(loginFailNum);
|
|
|
+ if (num >= 10){
|
|
|
+ model.addAttribute("maxCount","true");
|
|
|
+ message = "";
|
|
|
+ model.addAttribute(FormAuthenticationFilter.DEFAULT_MESSAGE_PARAM, message);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 验证失败清空验证码
|
|
|
- //request.getSession().setAttribute(ValidateCodeServlet.VALIDATE_CODE, IdGen.uuid());
|
|
|
- jedis = JedisUtils.getResource();
|
|
|
- jedis.set(ValidateCodeServlet.VALIDATE_CODE, IdGen.uuid());
|
|
|
+
|
|
|
|
|
|
// 如果是手机登录,则返回JSON字符串
|
|
|
if (mobile){
|
|
@@ -305,6 +324,17 @@ public class LoginController extends BaseController{
|
|
|
Principal principal = UserUtils.getPrincipal();
|
|
|
User user = UserUtils.getUser();
|
|
|
try {
|
|
|
+ //从redis中获取失败次数
|
|
|
+ String key = "loginFailNum:"+user.getLoginName();
|
|
|
+ String loginFailNum = JedisUtils.getResource().get(key);
|
|
|
+ if (StringUtils.isNotBlank(loginFailNum)){
|
|
|
+ int num = Integer.parseInt(loginFailNum);
|
|
|
+ if (num >= 10){
|
|
|
+ model.addAttribute("maxCount","true");
|
|
|
+ isValidateCodeLogin(user.getLoginName(), false, false);
|
|
|
+ return "modules/sys/sysLogin";
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 切换用户时更新用户的company,office
|
|
@@ -457,6 +487,20 @@ public class LoginController extends BaseController{
|
|
|
if (clean){
|
|
|
loginFailMap.remove(useruame);
|
|
|
}
|
|
|
+ // 保存到 Redis
|
|
|
+ JedisUtils.getResource().set("loginFailNum:"+useruame, loginFailNum.toString());
|
|
|
+
|
|
|
+ // 设置到第二天零点过期
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ LocalDateTime midnight = now.toLocalDate().plusDays(1).atStartOfDay();
|
|
|
+ long secondsUntilMidnight = Duration.between(now, midnight).getSeconds();
|
|
|
+ JedisUtils.getResource().expire("loginFailNum:"+useruame, (int) secondsUntilMidnight);
|
|
|
+
|
|
|
+ //用于测试
|
|
|
+// LocalDateTime nextExpiry = now.toLocalDate().atTime(14, 18);
|
|
|
+// long secondsUntilExpiry = Duration.between(now, nextExpiry).getSeconds();
|
|
|
+// JedisUtils.getResource().expire("loginFailNum:" + useruame, (int) secondsUntilExpiry);
|
|
|
+
|
|
|
return loginFailNum >= 5;
|
|
|
}
|
|
|
|