mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 09:35:29 +08:00
update 限流注解支持添加到类上面,对所有被代理的方法生效,方法上的注解优先
This commit is contained in:
parent
d53ccd0857
commit
ce5f613e8b
@ -9,7 +9,7 @@ import java.lang.annotation.*;
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface RateLimiter {
|
||||
|
||||
@ -19,6 +19,7 @@ import org.springframework.context.expression.BeanFactoryResolver;
|
||||
import org.springframework.context.expression.MethodBasedEvaluationContext;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.ParserContext;
|
||||
@ -50,8 +51,9 @@ public class RateLimiterAspect {
|
||||
private final ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
|
||||
|
||||
|
||||
@Before("@annotation(rateLimiter)")
|
||||
public void doBefore(JoinPoint point, RateLimiter rateLimiter) {
|
||||
@Before("@annotation(org.dromara.common.ratelimiter.annotation.RateLimiter) || @within(org.dromara.common.ratelimiter.annotation.RateLimiter)")
|
||||
public void doBefore(JoinPoint point) {
|
||||
RateLimiter rateLimiter = getRateLimiter(point);
|
||||
int time = rateLimiter.time();
|
||||
int count = rateLimiter.count();
|
||||
int timeout = rateLimiter.timeout();
|
||||
@ -79,6 +81,24 @@ public class RateLimiterAspect {
|
||||
}
|
||||
}
|
||||
|
||||
private RateLimiter getRateLimiter(JoinPoint joinPoint){
|
||||
RateLimiter rateLimiter = null;
|
||||
// 优先获取方法上的注解
|
||||
if (joinPoint.getSignature() instanceof MethodSignature signature) {
|
||||
rateLimiter = AnnotationUtils.findAnnotation(signature.getMethod(), RateLimiter.class);
|
||||
log.info("限流处理器拦截方法: {}\t方法是否具有限流注解:{}", signature.getMethod().getName(),rateLimiter != null);
|
||||
}
|
||||
|
||||
// 如果方法上不存在,则获取类上的注解
|
||||
if (rateLimiter == null) {
|
||||
rateLimiter = AnnotationUtils.findAnnotation(joinPoint.getTarget().getClass(), RateLimiter.class);
|
||||
log.info("限流处理器拦截类: {}\t类是否具有限流注解:{}", joinPoint.getTarget().getClass().getCanonicalName(),rateLimiter != null);
|
||||
}
|
||||
log.info("限流注解:{}",rateLimiter);
|
||||
// 因为处理条件是 @annotation(RateLimiter) || @within(RateLimiter) ,所以方法上的注解或者类上的注解必然会一个不为空,否则不会进入到此代理方法
|
||||
return rateLimiter;
|
||||
}
|
||||
|
||||
private String getCombineKey(RateLimiter rateLimiter, JoinPoint point) {
|
||||
String key = rateLimiter.key();
|
||||
// 判断 key 不为空 和 不是表达式
|
||||
|
||||
Loading…
Reference in New Issue
Block a user