频率限制8
This commit is contained in:
@@ -3,6 +3,7 @@ package com.sqx.common.aspect;
|
|||||||
import com.google.common.util.concurrent.RateLimiter;
|
import com.google.common.util.concurrent.RateLimiter;
|
||||||
import com.sqx.common.annotation.Limiting;
|
import com.sqx.common.annotation.Limiting;
|
||||||
import com.sqx.common.exception.CzgException;
|
import com.sqx.common.exception.CzgException;
|
||||||
|
import com.sqx.common.exception.LimitException;
|
||||||
import com.sqx.common.utils.IPUtils;
|
import com.sqx.common.utils.IPUtils;
|
||||||
import com.sqx.common.utils.Result;
|
import com.sqx.common.utils.Result;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -58,7 +59,7 @@ public class RateLimitAspect {
|
|||||||
} else {
|
} else {
|
||||||
// 未获取到许可,抛出异常或返回错误信息
|
// 未获取到许可,抛出异常或返回错误信息
|
||||||
log.info("IP: {} 请求方法: {} 超过访问频率限制", ip, method.getName());
|
log.info("IP: {} 请求方法: {} 超过访问频率限制", ip, method.getName());
|
||||||
throw new CzgException("访问频率过高,请稍后再试");
|
throw new LimitException("访问频率过高,请稍后再试");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/main/java/com/sqx/common/exception/LimitException.java
Normal file
10
src/main/java/com/sqx/common/exception/LimitException.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.sqx.common.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author GYJoker
|
||||||
|
*/
|
||||||
|
public class LimitException extends RuntimeException {
|
||||||
|
public LimitException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -79,6 +79,12 @@ public class SqxExceptionHandler {
|
|||||||
return new ResponseEntity<>(HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(LimitException.class)
|
||||||
|
public ResponseEntity<Void> handleLimitException() {
|
||||||
|
// 响应状态码为 444 表示无法处理的异常
|
||||||
|
return new ResponseEntity<>(HttpStatus.LOCKED);
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(IOException.class)
|
@ExceptionHandler(IOException.class)
|
||||||
public Result handleIoException() {
|
public Result handleIoException() {
|
||||||
return Result.error("请求中断,请重试");
|
return Result.error("请求中断,请重试");
|
||||||
|
|||||||
Reference in New Issue
Block a user