| | |
| | | package cn.mb.cloud.gateway.filter; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
| | | import org.springframework.cloud.gateway.filter.GlobalFilter; |
| | | import org.springframework.core.Ordered; |
| | | import org.springframework.core.io.buffer.*; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.http.server.reactive.ServerHttpRequestDecorator; |
| | | import org.springframework.http.server.reactive.ServerHttpResponse; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.server.ServerWebExchange; |
| | | import reactor.core.publisher.Flux; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Set; |
| | | |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { |
| | | |
| | | @Override |
| | | public int getOrder() { |
| | | return Ordered.HIGHEST_PRECEDENCE; |
| | | } |
| | | |
| | | @Override |
| | | public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| | | ServerHttpRequest req = exchange.getRequest(); |
| | | ServerHttpResponse res = exchange.getResponse(); |
| | | String method = req.getMethodValue(); |
| | | MediaType ct = req.getHeaders().getContentType(); |
| | | if (HttpMethod.POST.matches(method)) { |
| | | return DataBufferUtils.join(req.getBody()).flatMap(dataBuffer -> { |
| | | byte[] bytes = new byte[dataBuffer.readableByteCount()]; |
| | | dataBuffer.read(bytes); |
| | | // String bodyStr = new String(bytes, StandardCharsets.UTF_8); |
| | | // exchange.getAttributes().put("POST_BODY", bodyStr); |
| | | DataBufferUtils.release(dataBuffer); |
| | | Flux<DataBuffer> cachedFlux = Flux.defer(() -> { |
| | | DataBuffer buffer = res.bufferFactory().wrap(bytes); |
| | | return Mono.just(buffer); |
| | | }); |
| | | ServerHttpRequest mutateReq = new ServerHttpRequestDecorator(req) { |
| | | @Override |
| | | public Flux<DataBuffer> getBody() { |
| | | return cachedFlux; |
| | | } |
| | | }; |
| | | return chain.filter(exchange.mutate().request(mutateReq).build()); |
| | | }); |
| | | } |
| | | return chain.filter(exchange); |
| | | } |
| | | |
| | | } |
| | | //package cn.mb.cloud.gateway.filter; |
| | | // |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
| | | //import org.springframework.cloud.gateway.filter.GlobalFilter; |
| | | //import org.springframework.core.Ordered; |
| | | //import org.springframework.core.io.buffer.*; |
| | | //import org.springframework.http.HttpMethod; |
| | | //import org.springframework.http.MediaType; |
| | | //import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | //import org.springframework.http.server.reactive.ServerHttpRequestDecorator; |
| | | //import org.springframework.http.server.reactive.ServerHttpResponse; |
| | | //import org.springframework.stereotype.Component; |
| | | //import org.springframework.web.server.ServerWebExchange; |
| | | //import reactor.core.publisher.Flux; |
| | | //import reactor.core.publisher.Mono; |
| | | // |
| | | //import java.nio.charset.StandardCharsets; |
| | | //import java.util.Set; |
| | | // |
| | | // |
| | | //@Slf4j |
| | | //@Component |
| | | //public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { |
| | | // |
| | | // @Override |
| | | // public int getOrder() { |
| | | // return Ordered.HIGHEST_PRECEDENCE; |
| | | // } |
| | | // |
| | | // @Override |
| | | // public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| | | // ServerHttpRequest req = exchange.getRequest(); |
| | | // ServerHttpResponse res = exchange.getResponse(); |
| | | // String method = req.getMethodValue(); |
| | | // MediaType ct = req.getHeaders().getContentType(); |
| | | // if (HttpMethod.POST.matches(method)) { |
| | | // return DataBufferUtils.join(req.getBody()).flatMap(dataBuffer -> { |
| | | // byte[] bytes = new byte[dataBuffer.readableByteCount()]; |
| | | // dataBuffer.read(bytes); |
| | | //// String bodyStr = new String(bytes, StandardCharsets.UTF_8); |
| | | //// exchange.getAttributes().put("POST_BODY", bodyStr); |
| | | // DataBufferUtils.release(dataBuffer); |
| | | // Flux<DataBuffer> cachedFlux = Flux.defer(() -> { |
| | | // DataBuffer buffer = res.bufferFactory().wrap(bytes); |
| | | // return Mono.just(buffer); |
| | | // }); |
| | | // ServerHttpRequest mutateReq = new ServerHttpRequestDecorator(req) { |
| | | // @Override |
| | | // public Flux<DataBuffer> getBody() { |
| | | // return cachedFlux; |
| | | // } |
| | | // }; |
| | | // return chain.filter(exchange.mutate().request(mutateReq).build()); |
| | | // }); |
| | | // } |
| | | // return chain.filter(exchange); |
| | | // } |
| | | // |
| | | //} |