Commit 5f58dfdcd557918a54202a007172ab92bd1a9e90
1 parent
2c82556d
fix:AOP 拼接参数时,servlet 相关 request、response 可能无法序列化的问题
Showing
7 changed files
with
39 additions
and
12 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java
| @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.common.util.string; | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.common.util.string; | ||
| 3 | import cn.hutool.core.text.StrPool; | 3 | import cn.hutool.core.text.StrPool; |
| 4 | import cn.hutool.core.util.ArrayUtil; | 4 | import cn.hutool.core.util.ArrayUtil; |
| 5 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | +import org.aspectj.lang.JoinPoint; | ||
| 6 | 7 | ||
| 7 | import java.util.Arrays; | 8 | import java.util.Arrays; |
| 8 | import java.util.Collection; | 9 | import java.util.Collection; |
| @@ -77,4 +78,30 @@ public class StrUtils { | @@ -77,4 +78,30 @@ public class StrUtils { | ||
| 77 | .collect(Collectors.joining("\n")); | 78 | .collect(Collectors.joining("\n")); |
| 78 | } | 79 | } |
| 79 | 80 | ||
| 81 | + /** | ||
| 82 | + * 拼接方法的参数 | ||
| 83 | + * | ||
| 84 | + * 特殊:排除一些无法序列化的参数,如 ServletRequest、ServletResponse、MultipartFile | ||
| 85 | + * | ||
| 86 | + * @param joinPoint 连接点 | ||
| 87 | + * @return 拼接后的参数 | ||
| 88 | + */ | ||
| 89 | + public static String joinMethodArgs(JoinPoint joinPoint) { | ||
| 90 | + Object[] args = joinPoint.getArgs(); | ||
| 91 | + if (ArrayUtil.isEmpty(args)) { | ||
| 92 | + return ""; | ||
| 93 | + } | ||
| 94 | + return ArrayUtil.join(args, ",", item -> { | ||
| 95 | + if (item == null) { | ||
| 96 | + return ""; | ||
| 97 | + } | ||
| 98 | + // 讨论可见:https://t.zsxq.com/XUJVk、https://t.zsxq.com/MnKcL | ||
| 99 | + String clazzName = item.getClass().getName(); | ||
| 100 | + if (StrUtil.startWithAny(clazzName, "javax.servlet", "jakarta.servlet", "org.springframework.web")) { | ||
| 101 | + return ""; | ||
| 102 | + } | ||
| 103 | + return item; | ||
| 104 | + }); | ||
| 105 | + } | ||
| 106 | + | ||
| 80 | } | 107 | } |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/idempotent/core/keyresolver/impl/DefaultIdempotentKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 5 | import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent; | 5 | import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent; |
| 6 | import cn.iocoder.yudao.framework.idempotent.core.keyresolver.IdempotentKeyResolver; | 6 | import cn.iocoder.yudao.framework.idempotent.core.keyresolver.IdempotentKeyResolver; |
| 7 | import org.aspectj.lang.JoinPoint; | 7 | import org.aspectj.lang.JoinPoint; |
| @@ -18,7 +18,7 @@ public class DefaultIdempotentKeyResolver implements IdempotentKeyResolver { | @@ -18,7 +18,7 @@ public class DefaultIdempotentKeyResolver implements IdempotentKeyResolver { | ||
| 18 | @Override | 18 | @Override |
| 19 | public String resolver(JoinPoint joinPoint, Idempotent idempotent) { | 19 | public String resolver(JoinPoint joinPoint, Idempotent idempotent) { |
| 20 | String methodName = joinPoint.getSignature().toString(); | 20 | String methodName = joinPoint.getSignature().toString(); |
| 21 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 21 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 22 | return SecureUtil.md5(methodName + argsStr); | 22 | return SecureUtil.md5(methodName + argsStr); |
| 23 | } | 23 | } |
| 24 | 24 |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/idempotent/core/keyresolver/impl/UserIdempotentKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 5 | import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent; | 5 | import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent; |
| 6 | import cn.iocoder.yudao.framework.idempotent.core.keyresolver.IdempotentKeyResolver; | 6 | import cn.iocoder.yudao.framework.idempotent.core.keyresolver.IdempotentKeyResolver; |
| 7 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 7 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| @@ -19,7 +19,7 @@ public class UserIdempotentKeyResolver implements IdempotentKeyResolver { | @@ -19,7 +19,7 @@ public class UserIdempotentKeyResolver implements IdempotentKeyResolver { | ||
| 19 | @Override | 19 | @Override |
| 20 | public String resolver(JoinPoint joinPoint, Idempotent idempotent) { | 20 | public String resolver(JoinPoint joinPoint, Idempotent idempotent) { |
| 21 | String methodName = joinPoint.getSignature().toString(); | 21 | String methodName = joinPoint.getSignature().toString(); |
| 22 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 22 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 23 | Long userId = WebFrameworkUtils.getLoginUserId(); | 23 | Long userId = WebFrameworkUtils.getLoginUserId(); |
| 24 | Integer userType = WebFrameworkUtils.getLoginUserType(); | 24 | Integer userType = WebFrameworkUtils.getLoginUserType(); |
| 25 | return SecureUtil.md5(methodName + argsStr + userId + userType); | 25 | return SecureUtil.md5(methodName + argsStr + userId + userType); |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/ratelimiter/core/keyresolver/impl/ClientIpRateLimiterKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 5 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 4 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 5 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 6 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; | 6 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; |
| 7 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; | 7 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; |
| 8 | import org.aspectj.lang.JoinPoint; | 8 | import org.aspectj.lang.JoinPoint; |
| @@ -19,7 +19,7 @@ public class ClientIpRateLimiterKeyResolver implements RateLimiterKeyResolver { | @@ -19,7 +19,7 @@ public class ClientIpRateLimiterKeyResolver implements RateLimiterKeyResolver { | ||
| 19 | @Override | 19 | @Override |
| 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { | 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { |
| 21 | String methodName = joinPoint.getSignature().toString(); | 21 | String methodName = joinPoint.getSignature().toString(); |
| 22 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 22 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 23 | String clientIp = ServletUtils.getClientIP(); | 23 | String clientIp = ServletUtils.getClientIP(); |
| 24 | return SecureUtil.md5(methodName + argsStr + clientIp); | 24 | return SecureUtil.md5(methodName + argsStr + clientIp); |
| 25 | } | 25 | } |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/ratelimiter/core/keyresolver/impl/DefaultRateLimiterKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 5 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; | 5 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; |
| 6 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; | 6 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; |
| 7 | import org.aspectj.lang.JoinPoint; | 7 | import org.aspectj.lang.JoinPoint; |
| @@ -18,7 +18,7 @@ public class DefaultRateLimiterKeyResolver implements RateLimiterKeyResolver { | @@ -18,7 +18,7 @@ public class DefaultRateLimiterKeyResolver implements RateLimiterKeyResolver { | ||
| 18 | @Override | 18 | @Override |
| 19 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { | 19 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { |
| 20 | String methodName = joinPoint.getSignature().toString(); | 20 | String methodName = joinPoint.getSignature().toString(); |
| 21 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 21 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 22 | return SecureUtil.md5(methodName + argsStr); | 22 | return SecureUtil.md5(methodName + argsStr); |
| 23 | } | 23 | } |
| 24 | 24 |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/ratelimiter/core/keyresolver/impl/ServerNodeRateLimiterKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 5 | import cn.hutool.system.SystemUtil; | 4 | import cn.hutool.system.SystemUtil; |
| 5 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 6 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; | 6 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; |
| 7 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; | 7 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; |
| 8 | import org.aspectj.lang.JoinPoint; | 8 | import org.aspectj.lang.JoinPoint; |
| @@ -19,7 +19,7 @@ public class ServerNodeRateLimiterKeyResolver implements RateLimiterKeyResolver | @@ -19,7 +19,7 @@ public class ServerNodeRateLimiterKeyResolver implements RateLimiterKeyResolver | ||
| 19 | @Override | 19 | @Override |
| 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { | 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { |
| 21 | String methodName = joinPoint.getSignature().toString(); | 21 | String methodName = joinPoint.getSignature().toString(); |
| 22 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 22 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 23 | String serverNode = String.format("%s@%d", SystemUtil.getHostInfo().getAddress(), SystemUtil.getCurrentPID()); | 23 | String serverNode = String.format("%s@%d", SystemUtil.getHostInfo().getAddress(), SystemUtil.getCurrentPID()); |
| 24 | return SecureUtil.md5(methodName + argsStr + serverNode); | 24 | return SecureUtil.md5(methodName + argsStr + serverNode); |
| 25 | } | 25 | } |
yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/ratelimiter/core/keyresolver/impl/UserRateLimiterKeyResolver.java
| 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; | 1 | package cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.hutool.crypto.SecureUtil; | 3 | import cn.hutool.crypto.SecureUtil; |
| 4 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | ||
| 5 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; | 5 | import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter; |
| 6 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; | 6 | import cn.iocoder.yudao.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; |
| 7 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 7 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| @@ -19,7 +19,7 @@ public class UserRateLimiterKeyResolver implements RateLimiterKeyResolver { | @@ -19,7 +19,7 @@ public class UserRateLimiterKeyResolver implements RateLimiterKeyResolver { | ||
| 19 | @Override | 19 | @Override |
| 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { | 20 | public String resolver(JoinPoint joinPoint, RateLimiter rateLimiter) { |
| 21 | String methodName = joinPoint.getSignature().toString(); | 21 | String methodName = joinPoint.getSignature().toString(); |
| 22 | - String argsStr = StrUtil.join(",", joinPoint.getArgs()); | 22 | + String argsStr = StrUtils.joinMethodArgs(joinPoint); |
| 23 | Long userId = WebFrameworkUtils.getLoginUserId(); | 23 | Long userId = WebFrameworkUtils.getLoginUserId(); |
| 24 | Integer userType = WebFrameworkUtils.getLoginUserType(); | 24 | Integer userType = WebFrameworkUtils.getLoginUserType(); |
| 25 | return SecureUtil.md5(methodName + argsStr + userId + userType); | 25 | return SecureUtil.md5(methodName + argsStr + userId + userType); |