|
@@ -1,11 +1,14 @@
|
|
package cn.com.goldenwater.filter;
|
|
package cn.com.goldenwater.filter;
|
|
|
|
|
|
|
|
+import cn.com.goldenwater.domain.PtApp;
|
|
import cn.com.goldenwater.domain.PtService;
|
|
import cn.com.goldenwater.domain.PtService;
|
|
import cn.com.goldenwater.domain.PtServiceLog;
|
|
import cn.com.goldenwater.domain.PtServiceLog;
|
|
|
|
+import cn.com.goldenwater.service.PtAppService;
|
|
import cn.com.goldenwater.service.PtServiceLogService;
|
|
import cn.com.goldenwater.service.PtServiceLogService;
|
|
import cn.com.goldenwater.service.PtServiceService;
|
|
import cn.com.goldenwater.service.PtServiceService;
|
|
import cn.com.goldenwater.utils.GeoipUtils;
|
|
import cn.com.goldenwater.utils.GeoipUtils;
|
|
import cn.com.goldenwater.utils.JsonUtils;
|
|
import cn.com.goldenwater.utils.JsonUtils;
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.reactivestreams.Publisher;
|
|
import org.reactivestreams.Publisher;
|
|
@@ -22,6 +25,7 @@ import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
@@ -41,6 +45,9 @@ public class ResponseLogGlobalFilter implements GlobalFilter, Ordered {
|
|
@Autowired
|
|
@Autowired
|
|
private PtServiceService ptServiceService;
|
|
private PtServiceService ptServiceService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private PtAppService ptAppService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
try {
|
|
try {
|
|
@@ -94,7 +101,8 @@ public class ResponseLogGlobalFilter implements GlobalFilter, Ordered {
|
|
return bufferFactory.wrap(result.getBytes());
|
|
return bufferFactory.wrap(result.getBytes());
|
|
}));
|
|
}));
|
|
} else {
|
|
} else {
|
|
- log.error("<-- {} 响应code异常", getStatusCode());
|
|
|
|
|
|
+ // 对于非Flux响应体,仍然记录日志但不处理内容
|
|
|
|
+ saveServiceLog(exchange, "", System.currentTimeMillis() - startTime, originalResponse.getStatusCode().value());
|
|
}
|
|
}
|
|
return super.writeWith(body);
|
|
return super.writeWith(body);
|
|
}
|
|
}
|
|
@@ -126,7 +134,40 @@ public class ResponseLogGlobalFilter implements GlobalFilter, Ordered {
|
|
PtServiceLog serviceLog = new PtServiceLog();
|
|
PtServiceLog serviceLog = new PtServiceLog();
|
|
// 设置日志字段
|
|
// 设置日志字段
|
|
serviceLog.setTm(new Date());
|
|
serviceLog.setTm(new Date());
|
|
- serviceLog.setSenText(exchange.getRequest().getURI().toString());
|
|
|
|
|
|
+
|
|
|
|
+ // 构建包含查询参数和请求体的JSON对象
|
|
|
|
+ JSONObject requestParams = new JSONObject();
|
|
|
|
+
|
|
|
|
+ // 添加查询参数
|
|
|
|
+ MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
|
|
+ if (queryParams != null && !queryParams.isEmpty()) {
|
|
|
|
+ for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
|
|
|
|
+ List<String> values = entry.getValue();
|
|
|
|
+ if (values != null && !values.isEmpty()) {
|
|
|
|
+ if (values.size() == 1) {
|
|
|
|
+ requestParams.put(entry.getKey(), values.get(0));
|
|
|
|
+ } else {
|
|
|
|
+ requestParams.put(entry.getKey(), values);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加请求体参数(如果存在)
|
|
|
|
+ String requestBody = exchange.getAttribute("POST_BODY");
|
|
|
|
+ if (requestBody != null && !requestBody.isEmpty()) {
|
|
|
|
+ try {
|
|
|
|
+ JSONObject bodyJson = JSON.parseObject(requestBody);
|
|
|
|
+ requestParams.putAll(bodyJson);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ // 如果请求体不是JSON格式,将其作为普通字符串添加
|
|
|
|
+ requestParams.put("body", requestBody);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 将请求参数转换为JSON字符串存储
|
|
|
|
+ serviceLog.setSenText(requestParams.toJSONString());
|
|
|
|
+ serviceLog.setUrl(exchange.getRequest().getURI().getPath());
|
|
serviceLog.setReturnText(responseContent);
|
|
serviceLog.setReturnText(responseContent);
|
|
serviceLog.setExecTm(execTime);
|
|
serviceLog.setExecTm(execTime);
|
|
serviceLog.setStatusCode((long) statusCode);
|
|
serviceLog.setStatusCode((long) statusCode);
|
|
@@ -155,10 +196,19 @@ public class ResponseLogGlobalFilter implements GlobalFilter, Ordered {
|
|
serviceLog.setServiceName("未找到服务信息");
|
|
serviceLog.setServiceName("未找到服务信息");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 获取第三方服务ID
|
|
|
|
+ String thirdPartyId = (String) exchange.getAttribute("THIRD_PARTY_ID");
|
|
|
|
+ PtApp ptApp = ptAppService.getById(thirdPartyId);
|
|
|
|
+ if (ptApp != null) {
|
|
|
|
+ serviceLog.setUserId(ptApp.getUserId());
|
|
|
|
+ serviceLog.setAppId(ptApp.getAppId());
|
|
|
|
+ serviceLog.setAppName(ptApp.getAppName());
|
|
|
|
+ }
|
|
|
|
+
|
|
// 异步保存日志
|
|
// 异步保存日志
|
|
ptServiceLogService.save(serviceLog);
|
|
ptServiceLogService.save(serviceLog);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("保存服务日志异常: ", e);
|
|
log.error("保存服务日志异常: ", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|