fix: 拦截器增加异步
This commit is contained in:
parent
649f8f0c3f
commit
d496efb196
|
|
@ -1,6 +1,7 @@
|
||||||
package com.sqx.modules.app.interceptor;
|
package com.sqx.modules.app.interceptor;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.sqx.common.exception.CzgException;
|
import com.sqx.common.exception.CzgException;
|
||||||
import com.sqx.common.exception.SqxException;
|
import com.sqx.common.exception.SqxException;
|
||||||
|
|
@ -74,34 +75,40 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
long userId = Long.parseLong(claims.getSubject());
|
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)) {
|
if (redisService.checkIpJumpLimit(userId, ip)) {
|
||||||
log.warn("用户地址跳动频繁,封禁: {}", userId);
|
log.warn("用户地址跳动频繁,封禁: {}", userId);
|
||||||
if (!redisService.isSetUserState(userId)) {
|
if (!redisService.isSetUserState(userId)) {
|
||||||
userService.update(null, new LambdaUpdateWrapper<UserEntity>()
|
ThreadUtil.execAsync(() -> {
|
||||||
.eq(UserEntity::getUserId, userId)
|
userService.update(null, new LambdaUpdateWrapper<UserEntity>()
|
||||||
.set(UserEntity::getStatus, 0));
|
.eq(UserEntity::getUserId, userId)
|
||||||
|
.set(UserEntity::getStatus, 0));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
throw new CzgException("ip跳动过于频繁,请联系管理员解封");
|
throw new CzgException("ip跳动过于频繁,请联系管理员解封");
|
||||||
}
|
}
|
||||||
|
|
||||||
redisService.recordUrlVisitCountWithIp(userId, request.getRequestURI(), ip);
|
ThreadUtil.execAsync(() -> {
|
||||||
|
redisService.recordUrlVisitCountWithIp(userId, request.getRequestURI(), ip);
|
||||||
|
});
|
||||||
|
|
||||||
// 设置 userId 到 request 里,后续根据 userId 获取用户信息
|
// 设置 userId 到 request 里,后续根据 userId 获取用户信息
|
||||||
UserEntity user = userService.selectUserById(userId);
|
UserEntity user = userService.selectUserById(userId);
|
||||||
if (user != null && user.getStatus().equals(0)) {
|
if (user != null && user.getStatus().equals(0)) {
|
||||||
throw new CzgException("异常行为用户: {}" + user.getUserId());
|
throw new CzgException("异常行为用户: {}" + user.getUserId());
|
||||||
}
|
}
|
||||||
request.setAttribute(USER_KEY, userId);
|
|
||||||
|
|
||||||
if (redisService.isRecordUserOnLineTime(userId)) {
|
if (redisService.isRecordUserOnLineTime(userId)) {
|
||||||
// 记录用户最后一次调用接口的时间
|
ThreadUtil.execAsync(() -> {
|
||||||
UserEntity userEntity = new UserEntity();
|
// 记录用户最后一次调用接口的时间
|
||||||
userEntity.setUserId(userId);
|
UserEntity userEntity = new UserEntity();
|
||||||
userEntity.setOnLineTime(DateUtils.format(new Date()));
|
userEntity.setUserId(userId);
|
||||||
userService.updateById(userEntity);
|
userEntity.setOnLineTime(DateUtils.format(new Date()));
|
||||||
|
userService.updateById(userEntity);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue