频率限制8

This commit is contained in:
GYJ
2025-03-23 17:25:59 +08:00
parent 8ea8cf1dcd
commit 434641aefa
3 changed files with 18 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ 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 lombok.extern.slf4j.Slf4j;
@@ -58,7 +59,7 @@ public class RateLimitAspect {
} else {
// 未获取到许可,抛出异常或返回错误信息
log.info("IP: {} 请求方法: {} 超过访问频率限制", ip, method.getName());
throw new CzgException("访问频率过高,请稍后再试");
throw new LimitException("访问频率过高,请稍后再试");
}
}
}

View File

@@ -0,0 +1,10 @@
package com.sqx.common.exception;
/**
* @author GYJoker
*/
public class LimitException extends RuntimeException {
public LimitException(String message) {
super(message);
}
}

View File

@@ -79,6 +79,12 @@ public class SqxExceptionHandler {
return new ResponseEntity<>(HttpStatus.TOO_MANY_REQUESTS);
}
@ExceptionHandler(LimitException.class)
public ResponseEntity<Void> handleLimitException() {
// 响应状态码为 444 表示无法处理的异常
return new ResponseEntity<>(HttpStatus.LOCKED);
}
@ExceptionHandler(IOException.class)
public Result handleIoException() {
return Result.error("请求中断,请重试");