34 修复body数据过大超过默认256K时,报错(DataBufferLimitException: Exceeded limit on max bytes to buffer), 修改spring.codec.max-in-memory-size也无效的问题

pull/35/head
YunaiV 2023-05-29 00:00:08 +08:00
parent 5dd5bfb2c7
commit fcfd494747
1 changed files with 3 additions and 4 deletions

View File

@ -37,6 +37,7 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@ -57,10 +58,7 @@ import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MS_FORMATTER;
@Component @Component
public class AccessLogFilter implements GlobalFilter, Ordered { public class AccessLogFilter implements GlobalFilter, Ordered {
/** @Resource
* spring.codec.max-in-memory-size
*/
@Autowired
private CodecConfigurer codecConfigurer; private CodecConfigurer codecConfigurer;
/** /**
@ -141,6 +139,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
*/ */
private Mono<Void> filterWithRequestBody(ServerWebExchange exchange, GatewayFilterChain chain, AccessLog gatewayLog) { private Mono<Void> filterWithRequestBody(ServerWebExchange exchange, GatewayFilterChain chain, AccessLog gatewayLog) {
// 设置 Request Body 读取时,设置到网关日志 // 设置 Request Body 读取时,设置到网关日志
// 此处 codecConfigurer.getReaders() 的目的,是解决 spring.codec.max-in-memory-size 不生效
ServerRequest serverRequest = ServerRequest.create(exchange, codecConfigurer.getReaders()); ServerRequest serverRequest = ServerRequest.create(exchange, codecConfigurer.getReaders());
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> { Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
gatewayLog.setRequestBody(body); gatewayLog.setRequestBody(body);