ip 请求次数限制2

This commit is contained in:
GYJ 2025-03-23 20:02:46 +08:00
parent ac959c5015
commit f9d44fb4a4
2 changed files with 10 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package com.sqx.common.aspect;
import cn.hutool.core.thread.ThreadUtil;
import com.google.gson.Gson;
import com.sqx.common.exception.LimitException;
import com.sqx.common.utils.HttpContextUtils;
import com.sqx.common.utils.IPUtils;
import com.sqx.modules.common.service.CommonInfoService;
@ -50,6 +51,15 @@ 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("一分钟内请求超过限制IP: {}, 最大访问次数: {}", requestIp, maxAccessCount);
throw new LimitException("访问频率过高,请稍后再试");
}
String params = new Gson().toJson(args);
// 执行被拦截的方法
@ -57,10 +67,8 @@ public class AppApiMethodAspect {
long end = System.currentTimeMillis();
String value = commonInfoService.findOne(933).getValue();
if ("1".equals(value)) {
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
String method = request.getMethod();
String requestUrl = request.getRequestURL().toString();
String requestIp = IPUtils.getIpAddr(request);
long useTime = end - start;
ThreadUtil.execAsync(() -> {
//请求的参数

View File

@ -2,14 +2,11 @@ package com.sqx.common.aspect;
import com.google.common.util.concurrent.RateLimiter;
import com.sqx.common.annotation.Limiting;
import com.sqx.common.exception.CzgException;
import com.sqx.common.exception.LimitException;
import com.sqx.common.utils.IPUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.common.service.CommonInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@ -49,12 +46,6 @@ public class RateLimitAspect {
// 获取请求的 IP 地址
String ip = IPUtils.getIpAddr(request);
Integer maxAccessCount = Integer.parseInt(commonInfoService.findOne(935).getValue());
if (!IpAccessCounter.canAccess(ip, maxAccessCount)) {
log.info("一分钟内请求超过限制IP: {}, 最大访问次数: {}", ip, maxAccessCount);
throw new LimitException("访问频率过高,请稍后再试");
}
// 获取方法上的 @AccessLimit 注解
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();