Pre Merge pull request !885 from Ange_null/fix/rate-limiter-grpc-npe-5x

This commit is contained in:
Ange_null 2026-09-09 05:37:15 +00:00 committed by Gitee
commit f58d14aaaf
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -1,5 +1,6 @@
package org.dromara.common.ratelimiter.aspectj;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
@ -99,8 +100,14 @@ public class RateLimiterAspect {
key = expression.getValue(context, String.class);
}
StringBuilder stringBuffer = new StringBuilder(GlobalConstants.RATE_LIMIT_KEY);
stringBuffer.append(ServletUtils.getRequest().getRequestURI()).append(":");
if (rateLimiter.limitType() == LimitType.IP) {
// HTTP 线程gRPC消息消费异步任务等场景没有请求上下文getRequest() 返回 null
// 此处跳过 URI/IP 段仅以注解 key 限流否则 NPE 会导致被拦截方法每次调用都失败
// 业务侧表现为限流注解在非 HTTP 场景完全不可用
HttpServletRequest request = ServletUtils.getRequest();
if (request != null) {
stringBuffer.append(request.getRequestURI()).append(":");
}
if (rateLimiter.limitType() == LimitType.IP && request != null) {
// 获取请求ip
stringBuffer.append(ServletUtils.getClientIP()).append(":");
} else if (rateLimiter.limitType() == LimitType.CLUSTER) {