From 3421daa1e3ac3f1c79a91837ea592da66b5a2d2b Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 16 Apr 2021 17:49:19 +0800 Subject: [PATCH] fix limit on max bytes to buffer : 262144 --- .../gatewaycommon/gateway/ServerWebExchangeUtil.java | 12 ++++++++++++ .../gatewaycommon/gateway/filter/IndexFilter.java | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java index 19ad75c0..82a8d3a2 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java @@ -18,6 +18,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageReader; +import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.MultiValueMap; @@ -78,6 +79,17 @@ public class ServerWebExchangeUtil { return ServerRequest.create(exchange, messageReaders); } + /** + * 构建一个接受请求体的request + * + * @param exchange exchange + * @param codecConfigurer codecConfigurer + * @return 返回ServerRequest + */ + public static ServerRequest createReadBodyRequest(ServerWebExchange exchange, ServerCodecConfigurer codecConfigurer) { + return ServerRequest.create(exchange, codecConfigurer.getReaders()); + } + public static ApiParam getApiParamForRestful(ServerWebExchange exchange, String path) { int index = path.indexOf(REST_PATH); // 取"/rest"的后面部分 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java index 0d9801b9..9cb2c9fc 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java @@ -20,6 +20,7 @@ import org.springframework.core.annotation.Order; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; import org.springframework.web.reactive.function.BodyInserter; @@ -61,6 +62,9 @@ public class IndexFilter implements WebFilter { @Autowired private GatewayForwardChooser gatewayForwardChooser; + @Autowired + private ServerCodecConfigurer codecConfigurer; + @Override public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); @@ -84,7 +88,7 @@ public class IndexFilter implements WebFilter { } if (Objects.equals(path, indexPath)) { if (request.getMethod() == HttpMethod.POST) { - ServerRequest serverRequest = ServerWebExchangeUtil.createReadBodyRequest(exchange); + ServerRequest serverRequest = ServerWebExchangeUtil.createReadBodyRequest(exchange, codecConfigurer); // 读取请求体中的内容 Mono modifiedBody = serverRequest.bodyToMono(byte[].class) .flatMap(data -> {