ip 请求次数限制6
This commit is contained in:
@@ -61,13 +61,11 @@ 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<>());
|
||||||
// 移除超过一分钟的访问时间记录
|
// 移除超过一分钟的访问时间记录
|
||||||
if (accessTimes != null) {
|
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
||||||
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
if (accessTimes.size() + 1 > maxAccessCount) {
|
||||||
if (accessTimes.size() + 1 > maxAccessCount) {
|
addToBlacklist(ip);
|
||||||
addToBlacklist(ip);
|
log.info("IP {} 因一分钟内访问次数超过 {} 次,被加入黑名单", ip, maxAccessCount);
|
||||||
log.info("IP {} 因一分钟内访问次数超过 {} 次,被加入黑名单", ip, maxAccessCount);
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
accessTimes.add(currentTime);
|
accessTimes.add(currentTime);
|
||||||
|
|||||||
Reference in New Issue
Block a user