|
@@ -1,6 +1,9 @@
|
|
|
package com.fdkankan.gateway.filter;
|
|
|
|
|
|
+import cn.hutool.core.net.URLDecoder;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fdkankan.common.response.ResultData;
|
|
|
import com.fdkankan.gateway.log.GatewayLog;
|
|
|
import com.fdkankan.gateway.util.WebUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -68,7 +71,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
Route route = getGatewayRoute(exchange);
|
|
|
|
|
|
-
|
|
|
String ipAddress = WebUtil.getIpAddress(request);
|
|
|
|
|
|
GatewayLog gatewayLog = new GatewayLog();
|
|
@@ -81,7 +83,8 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
MediaType mediaType = request.getHeaders().getContentType();
|
|
|
|
|
|
- if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
|
|
|
+ if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
|
|
+ || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
|
|
|
return writeBodyLog(exchange, chain, gatewayLog);
|
|
|
}else{
|
|
|
return writeBasicLog(exchange, chain, gatewayLog);
|
|
@@ -120,7 +123,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)
|
|
|
.flatMap(body ->{
|
|
|
- gatewayLog.setRequestBody(body);
|
|
|
+ gatewayLog.setRequestBody(URLDecoder.decode(body, StandardCharsets.UTF_8));
|
|
|
return Mono.just(body);
|
|
|
});
|
|
|
|
|
@@ -136,6 +139,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
return bodyInserter.insert(outputMessage,new BodyInserterContext())
|
|
|
.then(Mono.defer(() -> {
|
|
|
+
|
|
|
// 重新封装请求
|
|
|
ServerHttpRequest decoratedRequest = requestDecorate(exchange, headers, outputMessage);
|
|
|
|
|
@@ -224,7 +228,8 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
if (this.getStatusCode().equals(HttpStatus.OK)
|
|
|
&& StrUtil.isNotBlank(originalResponseContentType)
|
|
|
- && originalResponseContentType.contains("application/json")) {
|
|
|
+// && originalResponseContentType.contains("application/json")
|
|
|
+ ) {
|
|
|
|
|
|
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
|
|
|
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
|
|
@@ -239,7 +244,10 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
DataBufferUtils.release(join);
|
|
|
String responseResult = new String(content, StandardCharsets.UTF_8);
|
|
|
|
|
|
- gatewayLog.setResponseData(responseResult);
|
|
|
+ ResultData resultData = JSON.parseObject(responseResult, ResultData.class);
|
|
|
+ gatewayLog.setCode(resultData.getCode());
|
|
|
+ gatewayLog.setMessage(resultData.getMessage());
|
|
|
+ gatewayLog.setResponseData(JSON.toJSONString(resultData.getData()));
|
|
|
|
|
|
|
|
|
return bufferFactory.wrap(content);
|