ip 请求次数限制12

This commit is contained in:
GYJ 2025-03-24 09:20:59 +08:00
parent 81c703eacc
commit de8385b8ca
2 changed files with 9 additions and 8 deletions

View File

@ -54,11 +54,11 @@ public class AppApiMethodAspect {
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
String requestIp = IPUtils.getIpAddr(request);
// Integer maxAccessCount = Integer.parseInt(commonInfoService.findOne(935).getValue());
// if (!IpAccessCounter.canAccess(requestIp, maxAccessCount)) {
// log.info("IpAccessCounter 一分钟内请求超过限制IP: {}, 最大访问次数: {}", requestIp, maxAccessCount);
// throw new LimitException("访问频率过高,请稍后再试");
// }
Integer maxAccessCount = Integer.parseInt(commonInfoService.findOne(935).getValue());
if (!IpAccessCounter.canAccess(requestIp, maxAccessCount)) {
log.info("IpAccessCounter 一分钟内请求超过限制IP: {}, 最大访问次数: {}", requestIp, maxAccessCount);
throw new LimitException("访问频率过高,请稍后再试");
}
String params = new Gson().toJson(args);

View File

@ -3,6 +3,7 @@ package com.sqx.common.aspect;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -14,9 +15,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class IpAccessCounter {
// 记录 IP 及其每次访问的时间列表
private static final Map<String, List<Long>> IP_ACCESS_TIMES = new HashMap<>();
private static final Map<String, List<Long>> IP_ACCESS_TIMES = new ConcurrentHashMap<>();
// 黑名单记录 IP 及其加入黑名单的时间
private static final Map<String, Long> BLACKLIST = new HashMap<>();
private static final Map<String, Long> BLACKLIST = new ConcurrentHashMap<>();
// 一分钟的毫秒数
private static final long ONE_MINUTE = 60 * 1000;
// 十分钟的毫秒数
@ -88,7 +89,7 @@ public class IpAccessCounter {
// 检查 IP 是否可以访问
public static boolean canAccess(String ip, Integer maxAccessCount) {
removeCount.getAndIncrement();
if (removeCount.get() > 300) {
if (removeCount.get() > 400) {
removeOldInfo();
}
if (isBlacklisted(ip)) {