From ad7133f39de9dcb2296fb32e2d90c26b762742a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 31 May 2022 05:03:34 +0000 Subject: [PATCH] =?UTF-8?q?update=20=E4=BD=BF=E7=94=A8=20gateway=20?= =?UTF-8?q?=E8=87=AA=E5=B8=A6=E5=B7=A5=E5=85=B7=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=BC=93=E5=AD=98Request=E4=B8=8Ebody?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/gateway/filter/CacheRequestFilter.java | 25 ++++++---------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java index d3481d8..621846b 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/CacheRequestFilter.java @@ -6,6 +6,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.OrderedGatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; @@ -58,26 +59,12 @@ public class CacheRequestFilter extends AbstractGatewayFilterFactory { - byte[] bytes = new byte[dataBuffer.readableByteCount()]; - dataBuffer.read(bytes); - DataBufferUtils.release(dataBuffer); - return bytes; - }).defaultIfEmpty(new byte[0]).flatMap(bytes -> { - DataBufferFactory dataBufferFactory = exchange.getResponse().bufferFactory(); - ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) + return ServerWebExchangeUtils.cacheRequestBodyAndRequest(exchange, (serverHttpRequest) -> { + if (serverHttpRequest == exchange.getRequest()) { - @Override - public Flux getBody() - { - if (bytes.length > 0) - { - return Flux.just(dataBufferFactory.wrap(bytes)); - } - return Flux.empty(); - } - }; - return chain.filter(exchange.mutate().request(decorator).build()); + return chain.filter(exchange); + } + return chain.filter(exchange.mutate().request(serverHttpRequest).build()); }); } }