ip 请求次数限制10
This commit is contained in:
parent
1309695fe2
commit
c188e51758
|
|
@ -6,6 +6,7 @@ import java.util.*;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author GYJoker
|
* @author GYJoker
|
||||||
|
|
@ -21,40 +22,75 @@ public class IpAccessCounter {
|
||||||
// 十分钟的毫秒数
|
// 十分钟的毫秒数
|
||||||
private static final long TEN_MINUTES = 10 * ONE_MINUTE;
|
private static final long TEN_MINUTES = 10 * ONE_MINUTE;
|
||||||
|
|
||||||
static {
|
private static AtomicInteger removeCount = new AtomicInteger(0);
|
||||||
// 定时任务,每分钟清理一次访问记录
|
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
|
||||||
executor.scheduleAtFixedRate(() -> {
|
|
||||||
try {
|
|
||||||
long currentTime = System.currentTimeMillis();
|
|
||||||
Set<String> removedIps = new HashSet<>();
|
|
||||||
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
|
|
||||||
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
|
||||||
if (accessTimes.isEmpty()) {
|
|
||||||
removedIps.add(ip);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
removedIps.forEach(IP_ACCESS_TIMES::remove);
|
|
||||||
|
|
||||||
Set<String> removedBlacklistIps = new HashSet<>();
|
// static {
|
||||||
BLACKLIST.entrySet().removeIf(entry -> {
|
// // 定时任务,每分钟清理一次访问记录
|
||||||
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
|
// ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||||
if (shouldRemove) {
|
// executor.scheduleAtFixedRate(() -> {
|
||||||
removedBlacklistIps.add(entry.getKey());
|
// try {
|
||||||
}
|
// long currentTime = System.currentTimeMillis();
|
||||||
return shouldRemove;
|
// Set<String> removedIps = new HashSet<>();
|
||||||
});
|
// IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
|
||||||
|
// accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
||||||
|
// if (accessTimes.isEmpty()) {
|
||||||
|
// removedIps.add(ip);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// removedIps.forEach(IP_ACCESS_TIMES::remove);
|
||||||
|
//
|
||||||
|
// Set<String> removedBlacklistIps = new HashSet<>();
|
||||||
|
// BLACKLIST.entrySet().removeIf(entry -> {
|
||||||
|
// boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
|
||||||
|
// if (shouldRemove) {
|
||||||
|
// removedBlacklistIps.add(entry.getKey());
|
||||||
|
// }
|
||||||
|
// return shouldRemove;
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
|
||||||
|
// removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error("定时任务异常", e);
|
||||||
|
// }
|
||||||
|
// }, ONE_MINUTE, 20000, TimeUnit.MINUTES);
|
||||||
|
// }
|
||||||
|
|
||||||
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
|
public static void removeOldInfo() {
|
||||||
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
|
try {
|
||||||
} catch (Exception e) {
|
removeCount.set(0);
|
||||||
log.error("定时任务异常", e);
|
long currentTime = System.currentTimeMillis();
|
||||||
}
|
Set<String> removedIps = new HashSet<>();
|
||||||
}, ONE_MINUTE, 20000, TimeUnit.MINUTES);
|
IP_ACCESS_TIMES.forEach((ip, accessTimes) -> {
|
||||||
|
accessTimes.removeIf(time -> currentTime - time > ONE_MINUTE);
|
||||||
|
if (accessTimes.isEmpty()) {
|
||||||
|
removedIps.add(ip);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
removedIps.forEach(IP_ACCESS_TIMES::remove);
|
||||||
|
|
||||||
|
Set<String> removedBlacklistIps = new HashSet<>();
|
||||||
|
BLACKLIST.entrySet().removeIf(entry -> {
|
||||||
|
boolean shouldRemove = currentTime - entry.getValue() > TEN_MINUTES;
|
||||||
|
if (shouldRemove) {
|
||||||
|
removedBlacklistIps.add(entry.getKey());
|
||||||
|
}
|
||||||
|
return shouldRemove;
|
||||||
|
});
|
||||||
|
|
||||||
|
log.info("IpAccessCounter 清理完成,移除了 {} 个 IP 的黑名单记录,当前 BLACKLIST 大小: {}, IP_ACCESS_TIMES 大小: {}",
|
||||||
|
removedBlacklistIps.size(), BLACKLIST.size(), IP_ACCESS_TIMES.size());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("定时任务异常", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查 IP 是否可以访问
|
// 检查 IP 是否可以访问
|
||||||
public static boolean canAccess(String ip, Integer maxAccessCount) {
|
public static boolean canAccess(String ip, Integer maxAccessCount) {
|
||||||
|
removeCount.getAndIncrement();
|
||||||
|
if (removeCount.get() > 200) {
|
||||||
|
removeOldInfo();
|
||||||
|
}
|
||||||
if (isBlacklisted(ip)) {
|
if (isBlacklisted(ip)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue