fix: 拦截器增加异步
This commit is contained in:
parent
649f8f0c3f
commit
d496efb196
|
|
@ -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)) {
|
||||
ThreadUtil.execAsync(() -> {
|
||||
userService.update(null, new LambdaUpdateWrapper<UserEntity>()
|
||||
.eq(UserEntity::getUserId, userId)
|
||||
.set(UserEntity::getStatus, 0));
|
||||
});
|
||||
}
|
||||
throw new CzgException("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)) {
|
||||
ThreadUtil.execAsync(() -> {
|
||||
// 记录用户最后一次调用接口的时间
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setUserId(userId);
|
||||
userEntity.setOnLineTime(DateUtils.format(new Date()));
|
||||
userService.updateById(userEntity);
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in New Issue