From 797244b8454cd98255766a1c157915b329b894de Mon Sep 17 00:00:00 2001 From: ych Date: Mon, 22 May 2023 15:45:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dbody=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=BF=87=E5=A4=A7=E8=B6=85=E8=BF=87=E9=BB=98=E8=AE=A4256K?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=8A=A5=E9=94=99(DataBufferLimitException:?= =?UTF-8?q?=20Exceeded=20limit=20on=20max=20bytes=20to=20buffer),=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9spring.codec.max-in-memory-size=E4=B9=9F?= =?UTF-8?q?=E6=97=A0=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/filter/logging/AccessLogFilter.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/logging/AccessLogFilter.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/logging/AccessLogFilter.java index 2a7ec84a7..984ee4108 100644 --- a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/logging/AccessLogFilter.java +++ b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/logging/AccessLogFilter.java @@ -9,6 +9,7 @@ import cn.iocoder.yudao.gateway.util.WebFrameworkUtils; import com.alibaba.nacos.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; import org.reactivestreams.Publisher; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage; @@ -23,7 +24,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; -import org.springframework.http.codec.HttpMessageReader; +import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; import org.springframework.http.server.reactive.ServerHttpResponse; @@ -31,7 +32,6 @@ import org.springframework.http.server.reactive.ServerHttpResponseDecorator; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; -import org.springframework.web.reactive.function.server.HandlerStrategies; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; @@ -57,7 +57,11 @@ import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MS_FORMATTER; @Component public class AccessLogFilter implements GlobalFilter, Ordered { - private final List> messageReaders = HandlerStrategies.withDefaults().messageReaders(); + /** + * 解决spring.codec.max-in-memory-size设置不生效的问题 + */ + @Autowired + private CodecConfigurer codecConfigurer; /** * 打印日志 @@ -137,7 +141,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered { */ private Mono filterWithRequestBody(ServerWebExchange exchange, GatewayFilterChain chain, AccessLog gatewayLog) { // 设置 Request Body 读取时,设置到网关日志 - ServerRequest serverRequest = ServerRequest.create(exchange, messageReaders); + ServerRequest serverRequest = ServerRequest.create(exchange, codecConfigurer.getReaders()); Mono modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> { gatewayLog.setRequestBody(body); return Mono.just(body);