From e65c67b55a982667accf1f31c6fbf346f4ed5825 Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Wed, 12 Mar 2025 11:14:18 +0800 Subject: [PATCH] =?UTF-8?q?ip=E9=99=90=E5=88=B6=E6=9A=82=E6=97=B6=E6=94=BE?= =?UTF-8?q?=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redisService/impl/RedisServiceImpl.java | 170 +++++++++--------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java b/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java index 9123a537..44e45123 100644 --- a/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java +++ b/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java @@ -158,91 +158,91 @@ public class RedisServiceImpl implements RedisService { @Override public boolean checkIpJumpLimit(long userId, String ip) { - // 存储用户上次的 IP 地址 - String userKey = "user:" + userId + ":last_ip"; - String lastIp = redisTemplate.opsForValue().get(userKey); - - // 获取用户跳动的历史 IP 地址集合 - String jumpHistoryKey = "user:" + userId + ":ip_history"; - Set jumpHistory = redisTemplate.opsForSet().members(jumpHistoryKey); - - // 记录 IP 跳动次数 - String ipTimestampKey = "user:" + userId + ":ip_timestamp"; - String userJumpCountKey = "user:" + userId + ":jump_count"; - - // 新增用于记录所有 IP 请求时间和次数的 Hash - String ipRequestsKey = "user:" + userId + ":ip_requests"; - - - // 如果用户之前没有记录过 IP,说明是第一次请求,直接保存 - if (lastIp == null) { - redisTemplate.opsForValue().set(userKey, ip); - - // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间 - redisTemplate.opsForSet().add(jumpHistoryKey, ip); - redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES); - - // 设置该 IP 的过期时间为 60 秒,表示跳动记录会在 60 秒后过期 - redisTemplate.opsForValue().set(ipTimestampKey + ":" + ip, "1", 1, TimeUnit.MINUTES); - - // 记录用户 IP 请求的次数和时间 - String ipData = DateUtil.date() + "|1"; // 时间和请求次数 - redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData); - - return false; - } - - // 如果当前 IP 和上次 IP 不一致,说明发生了 IP 跳动 - if (!lastIp.equals(ip)) { - // 判断当前 IP 是否在历史跳动的 IP 中 - if (jumpHistory != null && jumpHistory.contains(ip)) { - // 如果当前 IP 存在于历史跳动记录中,认为是多节点登录,增加跳动次数 - String s = redisTemplate.opsForValue().get(userJumpCountKey); - Integer currentJumpCount = s == null ? null : Integer.parseInt(s); - if (currentJumpCount == null) { - currentJumpCount = 0; - } - currentJumpCount++; - - // 更新跳动次数 - redisTemplate.opsForValue().set(userJumpCountKey, String.valueOf(currentJumpCount)); - - // 如果跳动次数超过限制,触发封禁 - if (currentJumpCount >= ipJumpLimit) { - return true; - } - } - - // 检查用户是否跳动过多次 - if (jumpHistory != null && jumpHistory.size() >= ipJumpLimit) { - // 如果历史 IP 地址数超过最大跳动次数,触发封禁 - return true; - } - - // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间 - redisTemplate.opsForSet().add(jumpHistoryKey, ip); - redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES); - - // 更新用户的上次 IP 地址 - redisTemplate.opsForValue().set(userKey, ip); - - // 增加该 IP 的跳动计数,并设置过期时间为 60 秒 - redisTemplate.opsForValue().increment(ipTimestampKey + ":" + ip, 1); - redisTemplate.expire(ipTimestampKey + ":" + ip, 1, TimeUnit.MINUTES); - - // 记录该 IP 请求的次数和时间 - String ipData = DateUtil.date() + "|1"; // 时间和请求次数 - redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData); - } else { - // 如果 IP 没有变化,更新该 IP 的请求次数和时间 - String currentCount = (String) redisTemplate.opsForHash().get(ipRequestsKey, ip); - String[] ipData = currentCount != null ? currentCount.split("\\|") : new String[]{"", "0"}; - int newCount = Integer.parseInt(ipData[1]) + 1; // 增加请求次数 - - // 更新新的时间和请求次数 - String updatedIpData = DateUtil.date() + "|" + newCount; - redisTemplate.opsForHash().put(ipRequestsKey, ip, updatedIpData); - } +// // 存储用户上次的 IP 地址 +// String userKey = "user:" + userId + ":last_ip"; +// String lastIp = redisTemplate.opsForValue().get(userKey); +// +// // 获取用户跳动的历史 IP 地址集合 +// String jumpHistoryKey = "user:" + userId + ":ip_history"; +// Set jumpHistory = redisTemplate.opsForSet().members(jumpHistoryKey); +// +// // 记录 IP 跳动次数 +// String ipTimestampKey = "user:" + userId + ":ip_timestamp"; +// String userJumpCountKey = "user:" + userId + ":jump_count"; +// +// // 新增用于记录所有 IP 请求时间和次数的 Hash +// String ipRequestsKey = "user:" + userId + ":ip_requests"; +// +// +// // 如果用户之前没有记录过 IP,说明是第一次请求,直接保存 +// if (lastIp == null) { +// redisTemplate.opsForValue().set(userKey, ip); +// +// // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间 +// redisTemplate.opsForSet().add(jumpHistoryKey, ip); +// redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES); +// +// // 设置该 IP 的过期时间为 60 秒,表示跳动记录会在 60 秒后过期 +// redisTemplate.opsForValue().set(ipTimestampKey + ":" + ip, "1", 1, TimeUnit.MINUTES); +// +// // 记录用户 IP 请求的次数和时间 +// String ipData = DateUtil.date() + "|1"; // 时间和请求次数 +// redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData); +// +// return false; +// } +// +// // 如果当前 IP 和上次 IP 不一致,说明发生了 IP 跳动 +// if (!lastIp.equals(ip)) { +// // 判断当前 IP 是否在历史跳动的 IP 中 +// if (jumpHistory != null && jumpHistory.contains(ip)) { +// // 如果当前 IP 存在于历史跳动记录中,认为是多节点登录,增加跳动次数 +// String s = redisTemplate.opsForValue().get(userJumpCountKey); +// Integer currentJumpCount = s == null ? null : Integer.parseInt(s); +// if (currentJumpCount == null) { +// currentJumpCount = 0; +// } +// currentJumpCount++; +// +// // 更新跳动次数 +// redisTemplate.opsForValue().set(userJumpCountKey, String.valueOf(currentJumpCount)); +// +// // 如果跳动次数超过限制,触发封禁 +// if (currentJumpCount >= ipJumpLimit) { +// return true; +// } +// } +// +// // 检查用户是否跳动过多次 +// if (jumpHistory != null && jumpHistory.size() >= ipJumpLimit) { +// // 如果历史 IP 地址数超过最大跳动次数,触发封禁 +// return true; +// } +// +// // 将当前 IP 添加到跳动历史中,设置 60 秒过期时间 +// redisTemplate.opsForSet().add(jumpHistoryKey, ip); +// redisTemplate.expire(jumpHistoryKey, 1, TimeUnit.MINUTES); +// +// // 更新用户的上次 IP 地址 +// redisTemplate.opsForValue().set(userKey, ip); +// +// // 增加该 IP 的跳动计数,并设置过期时间为 60 秒 +// redisTemplate.opsForValue().increment(ipTimestampKey + ":" + ip, 1); +// redisTemplate.expire(ipTimestampKey + ":" + ip, 1, TimeUnit.MINUTES); +// +// // 记录该 IP 请求的次数和时间 +// String ipData = DateUtil.date() + "|1"; // 时间和请求次数 +// redisTemplate.opsForHash().put(ipRequestsKey, ip, ipData); +// } else { +// // 如果 IP 没有变化,更新该 IP 的请求次数和时间 +// String currentCount = (String) redisTemplate.opsForHash().get(ipRequestsKey, ip); +// String[] ipData = currentCount != null ? currentCount.split("\\|") : new String[]{"", "0"}; +// int newCount = Integer.parseInt(ipData[1]) + 1; // 增加请求次数 +// +// // 更新新的时间和请求次数 +// String updatedIpData = DateUtil.date() + "|" + newCount; +// redisTemplate.opsForHash().put(ipRequestsKey, ip, updatedIpData); +// } return false; }