From ee69aa0a6de85a66b8d05e45e7076acebba33d5e Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Tue, 9 Jul 2019 03:08:10 +0800 Subject: [PATCH] =?UTF-8?q?webFlux=E6=98=AF=E7=9C=9F=E9=B8=A1=E5=B7=B4?= =?UTF-8?q?=E5=A4=8D=E6=9D=82.=20=E6=88=90=E5=8A=9F=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E5=88=B0body.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../factory/DubboGatewayFilterFactory.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java index bae7060..431adba 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java @@ -3,6 +3,7 @@ package cn.ecpark.service.usergw.biz.filters.factory; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -14,14 +15,20 @@ import org.apache.dubbo.rpc.service.GenericService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.cloud.gateway.support.DefaultServerRequest; import org.springframework.context.ApplicationContext; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpMethod; +import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; +import org.springframework.web.reactive.function.BodyExtractor; +import org.springframework.web.reactive.function.BodyInserter; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.ServerRequest; import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool; import lombok.extern.slf4j.Slf4j; @@ -58,9 +65,36 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory modifiedBody = serverRequest.bodyToMono(String.class).map(s -> { + log.info("original text:{}", s); + /** + * do your decrypt() here and convert to String.class + * or you can change the output class to change format + */ + return "foooooooo " + s; + }); + + modifiedBody.subscribe( databuferr -> { + log.info(databuferr); + }); + ServerHttpRequest req = exchange.getRequest(); Flux body = req.getBody(); + body.subscribe( databuferr -> { + byte[] bytes = new byte[databuferr.readableByteCount()]; + databuferr.read(bytes); + log.warn(bytes.toString()); + }); + + body.flatMap( databuferr->{ + byte[] bytes = new byte[databuferr.readableByteCount()]; + databuferr.read(bytes); + log.warn(bytes.toString()); + return Flux.empty(); + } ); MultiValueMap queryParams = req.getQueryParams();