ip限制暂时放开
This commit is contained in:
@@ -158,91 +158,91 @@ public class RedisServiceImpl implements RedisService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean checkIpJumpLimit(long userId, String ip) {
|
public boolean checkIpJumpLimit(long userId, String ip) {
|
||||||
// 存储用户上次的 IP 地址
|
// // 存储用户上次的 IP 地址
|
||||||
String userKey = "user:" + userId + ":last_ip";
|
// String userKey = "user:" + userId + ":last_ip";
|
||||||
String lastIp = redisTemplate.opsForValue().get(userKey);
|
// String lastIp = redisTemplate.opsForValue().get(userKey);
|
||||||
|
//
|
||||||
// 获取用户跳动的历史 IP 地址集合
|
// // 获取用户跳动的历史 IP 地址集合
|
||||||
String jumpHistoryKey = "user:" + userId + ":ip_history";
|
// String jumpHistoryKey = "user:" + userId + ":ip_history";
|
||||||
Set<String> jumpHistory = redisTemplate.opsForSet().members(jumpHistoryKey);
|
// Set<String> jumpHistory = redisTemplate.opsForSet().members(jumpHistoryKey);
|
||||||
|
//
|
||||||
// 记录 IP 跳动次数
|
// // 记录 IP 跳动次数
|
||||||
String ipTimestampKey = "user:" + userId + ":ip_timestamp";
|
// String ipTimestampKey = "user:" + userId + ":ip_timestamp";
|
||||||
String userJumpCountKey = "user:" + userId + ":jump_count";
|
// String userJumpCountKey = "user:" + userId + ":jump_count";
|
||||||
|
//
|
||||||
// 新增用于记录所有 IP 请求时间和次数的 Hash
|
// // 新增用于记录所有 IP 请求时间和次数的 Hash
|
||||||
String ipRequestsKey = "user:" + userId + ":ip_requests";
|
// String ipRequestsKey = "user:" + userId + ":ip_requests";
|
||||||
|
//
|
||||||
|
//
|
||||||
// 如果用户之前没有记录过 IP,说明是第一次请求,直接保存
|
// // 如果用户之前没有记录过 IP,说明是第一次请求,直接保存
|
||||||
if (lastIp == null) {
|
// if (lastIp == null) {
|
||||||
redisTemplate.opsForValue().set(userKey, ip);
|
// redisTemplate.opsForValue().set(userKey, ip);
|
||||||
|
//
|
||||||
// 将当前 IP 添加到跳动历史中,设置 60 秒过期时间
|
// // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间
|
||||||
redisTemplate.opsForSet().add(jumpHistoryKey, ip);
|
// redisTemplate.opsForSet().add(jumpHistoryKey, ip);
|
||||||
redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES);
|
// redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES);
|
||||||
|
//
|
||||||
// 设置该 IP 的过期时间为 60 秒,表示跳动记录会在 60 秒后过期
|
// // 设置该 IP 的过期时间为 60 秒,表示跳动记录会在 60 秒后过期
|
||||||
redisTemplate.opsForValue().set(ipTimestampKey + ":" + ip, "1", 1, TimeUnit.MINUTES);
|
// redisTemplate.opsForValue().set(ipTimestampKey + ":" + ip, "1", 1, TimeUnit.MINUTES);
|
||||||
|
//
|
||||||
// 记录用户 IP 请求的次数和时间
|
// // 记录用户 IP 请求的次数和时间
|
||||||
String ipData = DateUtil.date() + "|1"; // 时间和请求次数
|
// String ipData = DateUtil.date() + "|1"; // 时间和请求次数
|
||||||
redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData);
|
// redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData);
|
||||||
|
//
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 如果当前 IP 和上次 IP 不一致,说明发生了 IP 跳动
|
// // 如果当前 IP 和上次 IP 不一致,说明发生了 IP 跳动
|
||||||
if (!lastIp.equals(ip)) {
|
// if (!lastIp.equals(ip)) {
|
||||||
// 判断当前 IP 是否在历史跳动的 IP 中
|
// // 判断当前 IP 是否在历史跳动的 IP 中
|
||||||
if (jumpHistory != null && jumpHistory.contains(ip)) {
|
// if (jumpHistory != null && jumpHistory.contains(ip)) {
|
||||||
// 如果当前 IP 存在于历史跳动记录中,认为是多节点登录,增加跳动次数
|
// // 如果当前 IP 存在于历史跳动记录中,认为是多节点登录,增加跳动次数
|
||||||
String s = redisTemplate.opsForValue().get(userJumpCountKey);
|
// String s = redisTemplate.opsForValue().get(userJumpCountKey);
|
||||||
Integer currentJumpCount = s == null ? null : Integer.parseInt(s);
|
// Integer currentJumpCount = s == null ? null : Integer.parseInt(s);
|
||||||
if (currentJumpCount == null) {
|
// if (currentJumpCount == null) {
|
||||||
currentJumpCount = 0;
|
// currentJumpCount = 0;
|
||||||
}
|
// }
|
||||||
currentJumpCount++;
|
// currentJumpCount++;
|
||||||
|
//
|
||||||
// 更新跳动次数
|
// // 更新跳动次数
|
||||||
redisTemplate.opsForValue().set(userJumpCountKey, String.valueOf(currentJumpCount));
|
// redisTemplate.opsForValue().set(userJumpCountKey, String.valueOf(currentJumpCount));
|
||||||
|
//
|
||||||
// 如果跳动次数超过限制,触发封禁
|
// // 如果跳动次数超过限制,触发封禁
|
||||||
if (currentJumpCount >= ipJumpLimit) {
|
// if (currentJumpCount >= ipJumpLimit) {
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 检查用户是否跳动过多次
|
// // 检查用户是否跳动过多次
|
||||||
if (jumpHistory != null && jumpHistory.size() >= ipJumpLimit) {
|
// if (jumpHistory != null && jumpHistory.size() >= ipJumpLimit) {
|
||||||
// 如果历史 IP 地址数超过最大跳动次数,触发封禁
|
// // 如果历史 IP 地址数超过最大跳动次数,触发封禁
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 将当前 IP 添加到跳动历史中,设置 60 秒过期时间
|
// // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间
|
||||||
redisTemplate.opsForSet().add(jumpHistoryKey, ip);
|
// redisTemplate.opsForSet().add(jumpHistoryKey, ip);
|
||||||
redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES);
|
// redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES);
|
||||||
|
//
|
||||||
// 更新用户的上次 IP 地址
|
// // 更新用户的上次 IP 地址
|
||||||
redisTemplate.opsForValue().set(userKey, ip);
|
// redisTemplate.opsForValue().set(userKey, ip);
|
||||||
|
//
|
||||||
// 增加该 IP 的跳动计数,并设置过期时间为 60 秒
|
// // 增加该 IP 的跳动计数,并设置过期时间为 60 秒
|
||||||
redisTemplate.opsForValue().increment(ipTimestampKey + ":" + ip, 1);
|
// redisTemplate.opsForValue().increment(ipTimestampKey + ":" + ip, 1);
|
||||||
redisTemplate.expire(ipTimestampKey + ":" + ip, 1, TimeUnit.MINUTES);
|
// redisTemplate.expire(ipTimestampKey + ":" + ip, 1, TimeUnit.MINUTES);
|
||||||
|
//
|
||||||
// 记录该 IP 请求的次数和时间
|
// // 记录该 IP 请求的次数和时间
|
||||||
String ipData = DateUtil.date() + "|1"; // 时间和请求次数
|
// String ipData = DateUtil.date() + "|1"; // 时间和请求次数
|
||||||
redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData);
|
// redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData);
|
||||||
} else {
|
// } else {
|
||||||
// 如果 IP 没有变化,更新该 IP 的请求次数和时间
|
// // 如果 IP 没有变化,更新该 IP 的请求次数和时间
|
||||||
String currentCount = (String) redisTemplate.opsForHash().get(ipRequestsKey, ip);
|
// String currentCount = (String) redisTemplate.opsForHash().get(ipRequestsKey, ip);
|
||||||
String[] ipData = currentCount != null ? currentCount.split("\\|") : new String[]{"", "0"};
|
// String[] ipData = currentCount != null ? currentCount.split("\\|") : new String[]{"", "0"};
|
||||||
int newCount = Integer.parseInt(ipData[1]) + 1; // 增加请求次数
|
// int newCount = Integer.parseInt(ipData[1]) + 1; // 增加请求次数
|
||||||
|
//
|
||||||
// 更新新的时间和请求次数
|
// // 更新新的时间和请求次数
|
||||||
String updatedIpData = DateUtil.date() + "|" + newCount;
|
// String updatedIpData = DateUtil.date() + "|" + newCount;
|
||||||
redisTemplate.opsForHash().put(ipRequestsKey, ip, updatedIpData);
|
// redisTemplate.opsForHash().put(ipRequestsKey, ip, updatedIpData);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user