feat: 封禁用户状态仅更改一次

This commit is contained in:
张松
2024-12-30 13:14:59 +08:00
parent 6c02934e08
commit b8c0b8b2d9
3 changed files with 18 additions and 17 deletions

View File

@@ -79,9 +79,11 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
// 检查用户是否超过限流 // 检查用户是否超过限流
if (redisService.checkIpJumpLimit(userId, ip)) { if (redisService.checkIpJumpLimit(userId, ip)) {
log.warn("用户地址跳动频繁,封禁: {}", userId); log.warn("用户地址跳动频繁,封禁: {}", userId);
userService.update(null, new LambdaUpdateWrapper<UserEntity>() if (!redisService.isSetUserState(userId)) {
.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跳动过于频繁请联系管理员解封");
} }

View File

@@ -19,4 +19,6 @@ public interface RedisService {
void recordUrlVisitCountWithIp(long userId, String url, String ip); void recordUrlVisitCountWithIp(long userId, String url, String ip);
boolean isRecordUserOnLineTime(long userId); boolean isRecordUserOnLineTime(long userId);
boolean isSetUserState(long userId);
} }

View File

@@ -242,21 +242,18 @@ public class RedisServiceImpl implements RedisService {
return false; return false;
} }
@Override
public boolean isSetUserState(long userId) {
String userKey = "user:" + userId + ":state"; // 存储用户上次的 IP 地址
String key = redisUtils.get(userKey);
// 增加跳动次数的记录 if (StrUtil.isBlank(key)) {
private void increaseJumpCount(long userId) { DateTime date = DateUtil.date();
String jumpCountKey = "user:" + userId + ":jump_count"; // 跳动次数 Date tomorrow = DateUtil.beginOfDay(DateUtil.offsetDay(date, 1));
String jumpCount = redisTemplate.opsForValue().get(jumpCountKey); long expire = DateUtil.between(date, tomorrow, DateUnit.SECOND);
redisUtils.set(key, userId, expire);
if (jumpCount == null) { return false;
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);
} }
return true;
} }
// 记录用户访问 URL 的次数和 IP 地址 // 记录用户访问 URL 的次数和 IP 地址