|
@@ -1,4 +1,3 @@
|
|
|
-
|
|
|
package com.qunzhixinxi.hnqz.auth.handler;
|
|
|
|
|
|
import com.qunzhixinxi.hnqz.admin.api.entity.SysLog;
|
|
@@ -11,6 +10,7 @@ import com.qunzhixinxi.hnqz.common.security.handler.AuthenticationFailureHandler
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.security.core.Authentication;
|
|
@@ -19,6 +19,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author hnqz
|
|
@@ -29,33 +30,56 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
@AllArgsConstructor
|
|
|
public class HnqzAuthenticationFailureEventHandler implements AuthenticationFailureHandler {
|
|
|
|
|
|
- private final RemoteLogService logService;
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步处理,登录失败方法
|
|
|
- * <p>
|
|
|
- * @param authenticationException 登录的authentication 对象
|
|
|
- * @param authentication 登录的authenticationException 对象
|
|
|
- * @param request 请求
|
|
|
- * @param response 响应
|
|
|
- */
|
|
|
- @Async
|
|
|
- @Override
|
|
|
- @SneakyThrows
|
|
|
- public void handle(AuthenticationException authenticationException, Authentication authentication,
|
|
|
- HttpServletRequest request, HttpServletResponse response) {
|
|
|
- String username = authentication.getName();
|
|
|
- SysLog sysLog = SysLogUtils.getSysLog(request, username);
|
|
|
- sysLog.setTitle(username + "用户登录");
|
|
|
- sysLog.setType(CommonConstants.STATUS_LOCK);
|
|
|
- sysLog.setParams(username);
|
|
|
- sysLog.setException(authenticationException.getLocalizedMessage());
|
|
|
- String header = request.getHeader(HttpHeaders.AUTHORIZATION);
|
|
|
- sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
|
|
|
-
|
|
|
- logService.saveLog(sysLog, SecurityConstants.FROM_IN);
|
|
|
-
|
|
|
- log.info("用户:{} 登录失败,异常:{}", username, authenticationException.getLocalizedMessage());
|
|
|
- }
|
|
|
+ private final RemoteLogService logService;
|
|
|
+ private final RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步处理,登录失败方法
|
|
|
+ * <p>
|
|
|
+ *
|
|
|
+ * @param authenticationException 登录的authentication 对象
|
|
|
+ * @param authentication 登录的authenticationException 对象
|
|
|
+ * @param request 请求
|
|
|
+ * @param response 响应
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ @Override
|
|
|
+ @SneakyThrows
|
|
|
+ public void handle(AuthenticationException authenticationException, Authentication authentication,
|
|
|
+ HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ String username = authentication.getName();
|
|
|
+ SysLog sysLog = SysLogUtils.getSysLog(request, username);
|
|
|
+ sysLog.setTitle(username + "用户登录");
|
|
|
+ sysLog.setType(CommonConstants.STATUS_LOCK);
|
|
|
+ sysLog.setParams(username);
|
|
|
+ sysLog.setException(authenticationException.getLocalizedMessage());
|
|
|
+ String header = request.getHeader(HttpHeaders.AUTHORIZATION);
|
|
|
+ sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
|
|
|
+
|
|
|
+ logService.saveLog(sysLog, SecurityConstants.FROM_IN);
|
|
|
+
|
|
|
+ log.info("用户:{} 登录失败,异常:{}", username, authenticationException.getLocalizedMessage());
|
|
|
+
|
|
|
+ // 连续失败5次锁定10分钟
|
|
|
+
|
|
|
+ final String errorKey = "login:error:lock:" + username;
|
|
|
+ final String lockKey = "login:error:limit:times:" + username;
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ redisTemplate.opsForValue().set(lockKey, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|