From 2a46cdbd88db7bc0d6b5daa7edb74e14e6a30eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Sun, 26 Feb 2023 20:39:51 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E9=99=90?= =?UTF-8?q?=E6=B5=81=E5=8A=9F=E8=83=BD=20redis=20key=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E8=A7=84=E5=88=99=20=E4=BB=A5=E5=8A=9F=E8=83=BD=E5=A4=B4+url+i?= =?UTF-8?q?p+key=E6=A0=BC=E5=BC=8F=20fix=20=E4=BF=AE=E5=A4=8D=20!pr290=20?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/aspectj/RateLimiterAspect.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java index cc953b1fb..d558a4ce7 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java @@ -1,6 +1,8 @@ package com.ruoyi.framework.aspectj; +import cn.hutool.core.util.ArrayUtil; import com.ruoyi.common.annotation.RateLimiter; +import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.enums.LimitType; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.MessageUtils; @@ -35,16 +37,23 @@ import java.lang.reflect.Method; @Component public class RateLimiterAspect { - //定义spel表达式解析器 + /** + * 定义spel表达式解析器 + */ private final ExpressionParser parser = new SpelExpressionParser(); - //定义spel解析模版 + /** + * 定义spel解析模版 + */ private final ParserContext parserContext = new TemplateParserContext(); - //定义spel上下文对象进行解析 + /** + * 定义spel上下文对象进行解析 + */ private final EvaluationContext context = new StandardEvaluationContext(); - //方法参数解析器 + /** + * 方法参数解析器 + */ private final ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer(); - @Before("@annotation(rateLimiter)") public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable { int time = rateLimiter.time(); @@ -75,35 +84,38 @@ public class RateLimiterAspect { public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) { String key = rateLimiter.key(); - //获取方法(通过方法签名来获取) + // 获取方法(通过方法签名来获取) MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Class targetClass = method.getDeclaringClass(); - //判断是否是spel格式 + // 判断是否是spel格式 if (StringUtils.containsAny(key, "#")) { - //获取参数值 + // 获取参数值 Object[] args = point.getArgs(); - //获取方法上参数的名称 + // 获取方法上参数的名称 String[] parameterNames = pnd.getParameterNames(method); + if (ArrayUtil.isEmpty(parameterNames)) { + throw new ServiceException("限流key解析异常!请联系管理员!"); + } for (int i = 0; i < parameterNames.length; i++) { context.setVariable(parameterNames[i], args[i]); } - //解析返回给key + // 解析返回给key try { key = parser.parseExpression(key, parserContext).getValue(context, String.class) + ":"; } catch (Exception e) { throw new ServiceException("限流key解析异常!请联系管理员!"); } } - StringBuilder stringBuffer = new StringBuilder(key); + StringBuilder stringBuffer = new StringBuilder(CacheConstants.RATE_LIMIT_KEY); + stringBuffer.append(ServletUtils.getRequest().getRequestURI()).append(":"); if (rateLimiter.limitType() == LimitType.IP) { // 获取请求ip - stringBuffer.append(ServletUtils.getClientIP()).append("-"); + stringBuffer.append(ServletUtils.getClientIP()).append(":"); } else if (rateLimiter.limitType() == LimitType.CLUSTER) { // 获取客户端实例id - stringBuffer.append(RedisUtils.getClient().getId()).append("-"); + stringBuffer.append(RedisUtils.getClient().getId()).append(":"); } - stringBuffer.append(targetClass.getName()).append("-").append(method.getName()); - return stringBuffer.toString(); + return stringBuffer.append(key).toString(); } }