请求:
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.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import java.io.UnsupportedEncodingException;
import java.net.URI;/*** 在filter中获取前置预言里面的请求body*/
@Component
@Slf4j
public class WrapperRequestGlobalFilter implements GlobalFilter, Ordered {
/*** 优先级最高*/@Overridepublic int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;}@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();URI URIPath = request.getURI();String path = request.getPath().value();String method = request.getMethodValue();HttpHeaders header = request.getHeaders();log.info("");log.info("***********************************请求信息**********************************");log.info("请求request信息:URI = {}, path = {},method = {},header = {}。", URIPath, path, method, header);if ("POST".equals(method)) {
return DataBufferUtils.join(exchange.getRequest().getBody()).flatMap(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];dataBuffer.read(bytes);try {
String bodyString = new String(bytes, "utf-8");log.info("请求参数:" + bodyString);exchange.getAttributes().put("POST_BODY", bodyString);} catch (UnsupportedEncodingException e) {
e.printStackTrace();}DataBufferUtils.release(dataBuffer);Flux<DataBuffer> cachedFlux = Flux.defer(() -> {
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);return Mono.just(buffer);});ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
@Overridepublic Flux<DataBuffer> getBody() {
return cachedFlux;}};log.info("****************************************************************************\n");return chain.filter(exchange.mutate().request(mutatedRequest).build());});} else if ("GET".equals(method)) {
MultiValueMap<String, String> queryParams = request.getQueryParams();log.info("请求参数:" + queryParams);log.info("****************************************************************************\n");return chain.filter(exchange);}log.info("****************************************************************************\n");return chain.filter(exchange);}
}
响应:
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Set;@Component
@Slf4j
public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
@Overridepublic int getOrder() {
//-1 is response write filter, must be called before thatreturn -2;}@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//获取response的 返回数据ServerHttpResponse originalResponse = exchange.getResponse();DataBufferFactory bufferFactory = originalResponse.bufferFactory();ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Overridepublic Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = Flux.from(body);return super.writeWith(fluxBody.map(dataBuffer -> {
byte[] content = new byte[dataBuffer.readableByteCount()];dataBuffer.read(content);//释放掉内存DataBufferUtils.release(dataBuffer);//responseData就是下游系统返回的内容,可以查看修改String responseData = new String(content, Charset.forName("UTF-8"));log.info("");log.info("***********************************响应信息**********************************");log.info("响应内容:{}", responseData);log.info("****************************************************************************\n");byte[] uppedContent = new String(content, Charset.forName("UTF-8")).getBytes();return bufferFactory.wrap(uppedContent);}));} else {
log.error("响应code异常:{}", getStatusCode());}return super.writeWith(body);}};return chain.filter(exchange.mutate().response(decoratedResponse).build());}
}
喜欢本文的朋友不要忘记点一个免费的赞哦,你的赞将是我最大的动力。