Merge branch 'master-jdk21' of https://gitee.com/zhijiantianya/ruoyi-vue-pro
# Conflicts: # README.mdplp
commit
ceaf192bf2
@ -0,0 +1,55 @@
|
|||||||
|
package cn.iocoder.yudao.framework.ratelimiter.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.aop.RateLimiterAspect;
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver;
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl.*;
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.redis.RateLimiterRedisDAO;
|
||||||
|
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@AutoConfiguration(after = YudaoRedisAutoConfiguration.class)
|
||||||
|
public class YudaoRateLimiterConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RateLimiterAspect rateLimiterAspect(List<RateLimiterKeyResolver> keyResolvers, RateLimiterRedisDAO rateLimiterRedisDAO) {
|
||||||
|
return new RateLimiterAspect(keyResolvers, rateLimiterRedisDAO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||||
|
public RateLimiterRedisDAO rateLimiterRedisDAO(RedissonClient redissonClient) {
|
||||||
|
return new RateLimiterRedisDAO(redissonClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== 各种 RateLimiterRedisDAO Bean ==========
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DefaultRateLimiterKeyResolver defaultRateLimiterKeyResolver() {
|
||||||
|
return new DefaultRateLimiterKeyResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public UserRateLimiterKeyResolver userRateLimiterKeyResolver() {
|
||||||
|
return new UserRateLimiterKeyResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ClientIpRateLimiterKeyResolver clientIpRateLimiterKeyResolver() {
|
||||||
|
return new ClientIpRateLimiterKeyResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerNodeRateLimiterKeyResolver serverNodeRateLimiterKeyResolver() {
|
||||||
|
return new ServerNodeRateLimiterKeyResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ExpressionRateLimiterKeyResolver expressionRateLimiterKeyResolver() {
|
||||||
|
return new ExpressionRateLimiterKeyResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 限流 Key 解析器接口
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public interface RateLimiterKeyResolver {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析一个 Key
|
||||||
|
*
|
||||||
|
* @param rateLimiter 限流注解
|
||||||
|
* @param joinPoint AOP 切面
|
||||||
|
* @return Key
|
||||||
|
*/
|
||||||
|
String resolver(JoinPoint joinPoint, RateLimiter rateLimiter);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter;
|
||||||
|
import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||||
|
import org.springframework.core.ParameterNameDiscoverer;
|
||||||
|
import org.springframework.expression.Expression;
|
||||||
|
import org.springframework.expression.ExpressionParser;
|
||||||
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于 Spring EL 表达式的 {@link RateLimiterKeyResolver} 实现类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class ExpressionRateLimiterKeyResolver implements RateLimiterKeyResolver {
|
||||||
|
|
||||||
|
private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
||||||
|
|
||||||
|
private final ExpressionParser expressionParser = new SpelExpressionParser();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) {
|
||||||
|
// 获得被拦截方法参数名列表
|
||||||
|
Method method = getMethod(joinPoint);
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
String[] parameterNames = this.parameterNameDiscoverer.getParameterNames(method);
|
||||||
|
// 准备 Spring EL 表达式解析的上下文
|
||||||
|
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||||
|
if (ArrayUtil.isNotEmpty(parameterNames)) {
|
||||||
|
for (int i = 0; i < parameterNames.length; i++) {
|
||||||
|
evaluationContext.setVariable(parameterNames[i], args[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析参数
|
||||||
|
Expression expression = expressionParser.parseExpression(rateLimiter.keyArg());
|
||||||
|
return expression.getValue(evaluationContext, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Method getMethod(JoinPoint point) {
|
||||||
|
// 处理,声明在类上的情况
|
||||||
|
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||||
|
Method method = signature.getMethod();
|
||||||
|
if (!method.getDeclaringClass().isInterface()) {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理,声明在接口上的情况
|
||||||
|
try {
|
||||||
|
return point.getTarget().getClass().getDeclaredMethod(
|
||||||
|
point.getSignature().getName(), method.getParameterTypes());
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,2 +1,3 @@
|
|||||||
cn.iocoder.yudao.framework.idempotent.config.YudaoIdempotentConfiguration
|
cn.iocoder.yudao.framework.idempotent.config.YudaoIdempotentConfiguration
|
||||||
cn.iocoder.yudao.framework.lock4j.config.YudaoLock4jConfiguration
|
cn.iocoder.yudao.framework.lock4j.config.YudaoLock4jConfiguration
|
||||||
|
cn.iocoder.yudao.framework.ratelimiter.config.YudaoRateLimiterConfiguration
|
||||||
Loading…
Reference in New Issue