1、请求
package com.makeit.filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.net.URI;
import java.nio.charset.StandardCharsets;
/**
* @Date 2022/5/28
* @Time 01:09
* @Author Ealvis
* @Description Not Description
*/
@Component
@Slf4j
public class WrapperRequestGlobalFilter implements GlobalFilter, Ordered {
private static final String GET_METHOD = "GET";
private static final String POST_METHOD = "POST";
@Override
public 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("URI = {}", URIPath);
log.info("path = {}", path);
log.info("header = {}", header);
if(path.contains("diamond-web-file")){
return chain.filter(exchange);
}
String contentType = header.getFirst("content-type");
if (StringUtils.isNotBlank(contentType) && contentType.contains("multipart/form-data")) {
return chain.filter(exchange);
}
if (POST_METHOD.equals(method) && header.getContentLength() > 0) {
return DataBufferUtils.join(exchange.getRequest().getBody())
.flatMap(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
String bodyString = new String(bytes, StandardCharsets.UTF_8);
log.info("请求参数:" + bodyString);
exchange.getAttributes().put("POST_BODY", bodyString);
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()) {
@Override
public Flux<DataBuffer> getBody() {
return cachedFlux;
}
};
log.info("****************************************************************************\n");
return chain.filter(exchange.mutate().request(mutatedRequest)
.build());
});
} else if (GET_METHOD.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);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
2、响应
package com.makeit.filter;
import com.makeit.component.SysLogService;
import com.makeit.enums.SysLogEnum;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
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.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
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.Charset;
/**
* @Date 2022/5/28
* @Time 01:12
* @Author Ealvis
* @Description Not Description
*/
@Component
@Slf4j
public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
@Autowired
SysLogService sysLogService;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//获取response的 返回数据
ServerHttpRequest originalRequest = exchange.getRequest();
ServerHttpResponse originalResponse = exchange.getResponse();
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
HttpHeaders headers=getHeaders();
ContentDisposition contentDisposition=headers.getContentDisposition();
// 为附件的响应头 不打印响应日志
if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux && !contentDisposition.isAttachment()) {
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
String responseData = new String(content, Charset.forName("UTF-8"));
log.info("***********************************响应信息**********************************");
log.info("响应内容:{}", responseData);
log.info("****************************************************************************\n");
DataBufferUtils.release(join);
//byte[] uppedContent = responseData.getBytes();
sysLogService.save(originalRequest,null);
return bufferFactory.wrap(content);
}));
} else if(!contentDisposition.isAttachment()){
//保存日志
sysLogService.save(originalRequest, SysLogEnum.EXCEPTION.getCode());
log.error("响应code异常:{}", getStatusCode());
}
return super.writeWith(body);
}
};
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
@Override
public int getOrder() {
//-1是响应写入过滤器,必须在此之前调用
return -2;
}
}