From d496efb19648e4530c0699e37d99cafe544cd93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Mon, 30 Dec 2024 21:17:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8B=A6=E6=88=AA=E5=99=A8=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/AuthorizationInterceptor.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/sqx/modules/app/interceptor/AuthorizationInterceptor.java b/src/main/java/com/sqx/modules/app/interceptor/AuthorizationInterceptor.java index 1a886888..7e778a39 100644 --- a/src/main/java/com/sqx/modules/app/interceptor/AuthorizationInterceptor.java +++ b/src/main/java/com/sqx/modules/app/interceptor/AuthorizationInterceptor.java @@ -1,6 +1,7 @@ package com.sqx.modules.app.interceptor; +import cn.hutool.core.thread.ThreadUtil; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.sqx.common.exception.CzgException; import com.sqx.common.exception.SqxException; @@ -74,34 +75,40 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { } long userId = Long.parseLong(claims.getSubject()); - String ip = IPUtils.getIpAddr(request); // 获取用户的 IP 地址 + request.setAttribute(USER_KEY, userId); + String ip = IPUtils.getIpAddr(request); // 获取用户的 IP 地址 // 检查用户是否超过限流 if (redisService.checkIpJumpLimit(userId, ip)) { log.warn("用户地址跳动频繁,封禁: {}", userId); if (!redisService.isSetUserState(userId)) { - userService.update(null, new LambdaUpdateWrapper() - .eq(UserEntity::getUserId, userId) - .set(UserEntity::getStatus, 0)); + ThreadUtil.execAsync(() -> { + userService.update(null, new LambdaUpdateWrapper() + .eq(UserEntity::getUserId, userId) + .set(UserEntity::getStatus, 0)); + }); } throw new CzgException("ip跳动过于频繁,请联系管理员解封"); } - redisService.recordUrlVisitCountWithIp(userId, request.getRequestURI(), ip); + ThreadUtil.execAsync(() -> { + redisService.recordUrlVisitCountWithIp(userId, request.getRequestURI(), ip); + }); // 设置 userId 到 request 里,后续根据 userId 获取用户信息 UserEntity user = userService.selectUserById(userId); if (user != null && user.getStatus().equals(0)) { throw new CzgException("异常行为用户: {}" + user.getUserId()); } - request.setAttribute(USER_KEY, userId); if (redisService.isRecordUserOnLineTime(userId)) { - // 记录用户最后一次调用接口的时间 - UserEntity userEntity = new UserEntity(); - userEntity.setUserId(userId); - userEntity.setOnLineTime(DateUtils.format(new Date())); - userService.updateById(userEntity); + ThreadUtil.execAsync(() -> { + // 记录用户最后一次调用接口的时间 + UserEntity userEntity = new UserEntity(); + userEntity.setUserId(userId); + userEntity.setOnLineTime(DateUtils.format(new Date())); + userService.updateById(userEntity); + }); } return true;