feat: 封禁用户状态仅更改一次
This commit is contained in:
@@ -79,9 +79,11 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
|
||||
// 检查用户是否超过限流
|
||||
if (redisService.checkIpJumpLimit(userId, ip)) {
|
||||
log.warn("用户地址跳动频繁,封禁: {}", userId);
|
||||
userService.update(null, new LambdaUpdateWrapper<UserEntity>()
|
||||
.eq(UserEntity::getUserId, userId)
|
||||
.set(UserEntity::getStatus, 0));
|
||||
if (!redisService.isSetUserState(userId)) {
|
||||
userService.update(null, new LambdaUpdateWrapper<UserEntity>()
|
||||
.eq(UserEntity::getUserId, userId)
|
||||
.set(UserEntity::getStatus, 0));
|
||||
}
|
||||
throw new CzgException("ip跳动过于频繁,请联系管理员解封");
|
||||
}
|
||||
|
||||
|
||||
@@ -19,4 +19,6 @@ public interface RedisService {
|
||||
void recordUrlVisitCountWithIp(long userId, String url, String ip);
|
||||
|
||||
boolean isRecordUserOnLineTime(long userId);
|
||||
|
||||
boolean isSetUserState(long userId);
|
||||
}
|
||||
|
||||
@@ -242,21 +242,18 @@ public class RedisServiceImpl implements RedisService {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 增加跳动次数的记录
|
||||
private void increaseJumpCount(long userId) {
|
||||
String jumpCountKey = "user:" + userId + ":jump_count"; // 跳动次数
|
||||
String jumpCount = redisTemplate.opsForValue().get(jumpCountKey);
|
||||
|
||||
if (jumpCount == null) {
|
||||
redisTemplate.opsForValue().set(jumpCountKey, "1", 1, TimeUnit.MINUTES); // 初始跳动
|
||||
} else {
|
||||
int newJumpCount = Integer.parseInt(jumpCount) + 1;
|
||||
redisTemplate.opsForValue().set(jumpCountKey, String.valueOf(newJumpCount), 1, TimeUnit.MINUTES);
|
||||
@Override
|
||||
public boolean isSetUserState(long userId) {
|
||||
String userKey = "user:" + userId + ":state"; // 存储用户上次的 IP 地址
|
||||
String key = redisUtils.get(userKey);
|
||||
if (StrUtil.isBlank(key)) {
|
||||
DateTime date = DateUtil.date();
|
||||
Date tomorrow = DateUtil.beginOfDay(DateUtil.offsetDay(date, 1));
|
||||
long expire = DateUtil.between(date, tomorrow, DateUnit.SECOND);
|
||||
redisUtils.set(key, userId, expire);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 记录用户访问 URL 的次数和 IP 地址
|
||||
|
||||
Reference in New Issue
Block a user