ip 请求次数限制5

This commit is contained in:
GYJ 2025-03-23 21:07:27 +08:00
parent 383f08ef4d
commit 029d6e5562
1 changed files with 8 additions and 5 deletions

View File

@ -61,12 +61,15 @@ public class IpAccessCounter {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
List<Long> accessTimes = IP_ACCESS_TIMES.computeIfAbsent(ip, k -> new ArrayList<>()); List<Long> accessTimes = IP_ACCESS_TIMES.computeIfAbsent(ip, k -> new ArrayList<>());
// 移除超过一分钟的访问时间记录 // 移除超过一分钟的访问时间记录
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE); if (accessTimes != null) {
if (accessTimes.size() + 1 > maxAccessCount) { accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
addToBlacklist(ip); if (accessTimes.size() + 1 > maxAccessCount) {
log.info("IP {} 因一分钟内访问次数超过 {} 次,被加入黑名单", ip, maxAccessCount); addToBlacklist(ip);
return false; log.info("IP {} 因一分钟内访问次数超过 {} 次,被加入黑名单", ip, maxAccessCount);
return false;
}
} }
accessTimes.add(currentTime); accessTimes.add(currentTime);
return true; return true;
} }