diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java index 12731edad6..4d6f3cae36 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java @@ -77,6 +77,12 @@ public class ServletUtils { public static String getBody(HttpServletRequest request) { // 只有在 json 请求在读取,因为只有 CacheRequestBodyFilter 才会进行缓存,支持重复读取 if (isJsonRequest(request)) { + // 优先从请求属性中获取缓存的请求体 + String body = (String) request.getAttribute("cache_body"); + if (StrUtil.isNotEmpty(body)) { + return body; + } + return JakartaServletUtil.getBody(request); } return null; diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyFilter.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyFilter.java index 9071998f91..9c867494c1 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyFilter.java @@ -19,7 +19,10 @@ public class CacheRequestBodyFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { - filterChain.doFilter(new CacheRequestBodyWrapper(request), response); + CacheRequestBodyWrapper cacheRequestBodyWrapper = new CacheRequestBodyWrapper(request); + String body = ServletUtils.getBody(cacheRequestBodyWrapper); + request.setAttribute(CacheRequestBodyWrapper.CACHE_BODY, body); + filterChain.doFilter(cacheRequestBodyWrapper, response); } @Override diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyWrapper.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyWrapper.java index 8e80fa591f..775ee91d30 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyWrapper.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/filter/CacheRequestBodyWrapper.java @@ -18,6 +18,11 @@ import java.io.InputStreamReader; */ public class CacheRequestBodyWrapper extends HttpServletRequestWrapper { + /** + * 缓存在 request.attribute 的 body + */ + public static final String CACHE_BODY = "cache_body"; + /** * 缓存的内容 */ @@ -26,6 +31,9 @@ public class CacheRequestBodyWrapper extends HttpServletRequestWrapper { public CacheRequestBodyWrapper(HttpServletRequest request) { super(request); body = ServletUtils.getBodyBytes(request); + if (body != null) { + request.setAttribute(CacheRequestBodyWrapper.CACHE_BODY, new String(body)); + } } @Override