From fba7ffe77c5b9295b06b177cef3025b6d47ed48a Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期一, 06 二月 2023 11:40:33 +0800 Subject: [PATCH] 修复匿名注解Anonymous空指针问题(I683DT) --- src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java | 40 +++++++--------------------------------- 1 files changed, 7 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java b/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java index c31a67e..1f47d7a 100644 --- a/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java +++ b/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java @@ -4,10 +4,8 @@ import java.util.Collections; import java.util.List; import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,17 +47,9 @@ this.limitScript = limitScript; } - // 閰嶇疆缁囧叆鐐� - @Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.RateLimiter)") - public void rateLimiterPointCut() + @Before("@annotation(rateLimiter)") + public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable { - } - - @Before("rateLimiterPointCut()") - public void doBefore(JoinPoint point) throws Throwable - { - RateLimiter rateLimiter = getAnnotationRateLimiter(point); - String key = rateLimiter.key(); int time = rateLimiter.time(); int count = rateLimiter.count(); @@ -70,9 +60,9 @@ Long number = redisTemplate.execute(limitScript, keys, count, time); if (StringUtils.isNull(number) || number.intValue() > count) { - throw new ServiceException("璁块棶杩囦簬棰戠箒锛岃绋嶅悗鍐嶈瘯"); + throw new ServiceException("璁块棶杩囦簬棰戠箒锛岃绋嶅�欏啀璇�"); } - log.info("闄愬埗璇锋眰'{}',褰撳墠璇锋眰'{}',缂撳瓨key'{}'", count, number.intValue(), key); + log.info("闄愬埗璇锋眰'{}',褰撳墠璇锋眰'{}',缂撳瓨key'{}'", count, number.intValue(), combineKey); } catch (ServiceException e) { @@ -80,24 +70,8 @@ } catch (Exception e) { - throw new RuntimeException("鏈嶅姟鍣ㄩ檺娴佸紓甯革紝璇风◢鍚庡啀璇�"); + throw new RuntimeException("鏈嶅姟鍣ㄩ檺娴佸紓甯革紝璇风◢鍊欏啀璇�"); } - } - - /** - * 鏄惁瀛樺湪娉ㄨВ锛屽鏋滃瓨鍦ㄥ氨鑾峰彇 - */ - private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint) - { - Signature signature = joinPoint.getSignature(); - MethodSignature methodSignature = (MethodSignature) signature; - Method method = methodSignature.getMethod(); - - if (method != null) - { - return method.getAnnotation(RateLimiter.class); - } - return null; } public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) @@ -105,12 +79,12 @@ StringBuffer stringBuffer = new StringBuffer(rateLimiter.key()); if (rateLimiter.limitType() == LimitType.IP) { - stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest())); + stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest())).append("-"); } MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Class<?> targetClass = method.getDeclaringClass(); - stringBuffer.append("-").append(targetClass.getName()).append("- ").append(method.getName()); + stringBuffer.append(targetClass.getName()).append("-").append(method.getName()); return stringBuffer.toString(); } } -- Gitblit v1.9.3