|
@@ -10,7 +10,9 @@ import com.fdkankan.common.response.ResultData;
|
|
import com.fdkankan.gateway.log.GatewayLog;
|
|
import com.fdkankan.gateway.log.GatewayLog;
|
|
import com.fdkankan.gateway.util.WebUtil;
|
|
import com.fdkankan.gateway.util.WebUtil;
|
|
import com.yomahub.tlog.context.TLogContext;
|
|
import com.yomahub.tlog.context.TLogContext;
|
|
|
|
+import java.io.IOException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.reactivestreams.Publisher;
|
|
import org.reactivestreams.Publisher;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
@@ -99,14 +101,64 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
MediaType mediaType = request.getHeaders().getContentType();
|
|
MediaType mediaType = request.getHeaders().getContentType();
|
|
|
|
|
|
if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
|
if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
|
- || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)
|
|
|
|
- || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)){
|
|
|
|
|
|
+ || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
|
|
return writeBodyLog(exchange, chain, gatewayLog);
|
|
return writeBodyLog(exchange, chain, gatewayLog);
|
|
- }else{
|
|
|
|
|
|
+ }else if(MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)){
|
|
|
|
+ return readFormData(exchange, chain, gatewayLog);
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
return writeBasicLog(exchange, chain, gatewayLog);
|
|
return writeBasicLog(exchange, chain, gatewayLog);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private Mono<Void> readFormData(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog gatewayLog) {
|
|
|
|
+ ServerRequest serverRequest = ServerRequest.create(exchange,serverCodecConfigurer.getReaders());
|
|
|
|
+
|
|
|
|
+ Mono<DataBuffer> modifiedBody = serverRequest.bodyToMono(DataBuffer.class)
|
|
|
|
+ .flatMap(body ->{
|
|
|
|
+ try {
|
|
|
|
+ final byte[] bytes = IOUtils.toByteArray(body.asInputStream());
|
|
|
|
+ String s = new String(bytes);
|
|
|
|
+ System.out.println(123);
|
|
|
|
+// gatewayLog.setRequestBody(URLDecoder.decode(body, StandardCharsets.UTF_8));
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return Mono.just(body);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 通过 BodyInserter 插入 body(支持修改body), 避免 request body 只能获取一次
|
|
|
|
+ BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, DataBuffer.class);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.putAll(exchange.getRequest().getHeaders());
|
|
|
|
+ // the new content type will be computed by bodyInserter
|
|
|
|
+ // and then set in the request decorator
|
|
|
|
+ headers.remove(HttpHeaders.CONTENT_LENGTH);
|
|
|
|
+
|
|
|
|
+ CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
|
|
|
|
+
|
|
|
|
+ return bodyInserter.insert(outputMessage,new BodyInserterContext())
|
|
|
|
+ .then(Mono.defer(() -> {
|
|
|
|
+
|
|
|
|
+ // 重新封装请求
|
|
|
|
+ ServerHttpRequest decoratedRequest = requestDecorate(exchange, headers, outputMessage);
|
|
|
|
+
|
|
|
|
+ // 记录响应日志
|
|
|
|
+ ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, gatewayLog);
|
|
|
|
+
|
|
|
|
+ // 记录普通的
|
|
|
|
+ return chain.filter(exchange.mutate().request(decoratedRequest).response(decoratedResponse).build())
|
|
|
|
+ .doOnError(e->{
|
|
|
|
+ // 打印日志
|
|
|
|
+ writeBadAccessLog(gatewayLog, e);
|
|
|
|
+ })
|
|
|
|
+ .then(Mono.fromRunnable(() -> {
|
|
|
|
+ // 打印日志
|
|
|
|
+ writeAccessLog(gatewayLog);
|
|
|
|
+ }));
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
|
|
+
|
|
private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog accessLog) {
|
|
private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog accessLog) {
|
|
StringBuilder builder = new StringBuilder();
|
|
StringBuilder builder = new StringBuilder();
|
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
@@ -196,7 +248,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
private void writeAccessLog(GatewayLog gatewayLog) {
|
|
private void writeAccessLog(GatewayLog gatewayLog) {
|
|
log.info(JSON.toJSONString(gatewayLog));
|
|
log.info(JSON.toJSONString(gatewayLog));
|
|
//日志写入mongodb
|
|
//日志写入mongodb
|
|
- mongoTemplate.insert(gatewayLog, "gatewayLog");
|
|
|
|
|
|
+// mongoTemplate.insert(gatewayLog, "gatewayLog");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|