|
@@ -1,5 +1,7 @@
|
|
package com.example.config.web;
|
|
package com.example.config.web;
|
|
|
|
|
|
|
|
+import com.example.entity.common.RequestLogPo;
|
|
|
|
+import com.example.service.common.RequestLogService;
|
|
import com.example.utils.AESUtil;
|
|
import com.example.utils.AESUtil;
|
|
import com.example.utils.R;
|
|
import com.example.utils.R;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@@ -20,6 +22,7 @@ import java.net.URLDecoder;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -34,6 +37,11 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
* 计时器线程变量
|
|
* 计时器线程变量
|
|
*/
|
|
*/
|
|
private static final ThreadLocal<StopWatch> STOP_WATCH_THREAD_LOCAL = new ThreadLocal<>();
|
|
private static final ThreadLocal<StopWatch> STOP_WATCH_THREAD_LOCAL = new ThreadLocal<>();
|
|
|
|
+ private RequestLogService requestLogService;
|
|
|
|
+
|
|
|
|
+ public RequestLogHandlerInterceptor(RequestLogService requestLogService) {
|
|
|
|
+ this.requestLogService = requestLogService;
|
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
@@ -69,7 +77,6 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
body = StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
|
|
body = StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
|
|
log.info("请求体参数: {}", StringUtils.trimAllWhitespace(body));
|
|
log.info("请求体参数: {}", StringUtils.trimAllWhitespace(body));
|
|
}
|
|
}
|
|
- // todo: 记录日志
|
|
|
|
String token = request.getHeader("token");
|
|
String token = request.getHeader("token");
|
|
if (!StringUtils.hasText(token) || "undefined".equals(token)) {
|
|
if (!StringUtils.hasText(token) || "undefined".equals(token)) {
|
|
token = parameters.get("token");
|
|
token = parameters.get("token");
|
|
@@ -89,20 +96,39 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
// if (!"FINANCE".equals(map.get("APP_ID")) || !StringUtils.hasText(map.get("LOGIN_ID"))) {
|
|
// if (!"FINANCE".equals(map.get("APP_ID")) || !StringUtils.hasText(map.get("LOGIN_ID"))) {
|
|
// return forbidden(response);
|
|
// return forbidden(response);
|
|
// }
|
|
// }
|
|
- map.put("api", request.getRequestURI());
|
|
|
|
- map.put("body", body);
|
|
|
|
- map.put("time", now.toString());
|
|
|
|
- map.put("headers", gson.toJson(headers));
|
|
|
|
- map.put("parameters", gson.toJson(parameters));
|
|
|
|
- map.put("token", urlDecodeToken);
|
|
|
|
- log.info("调用记录: {}", gson.toJson(map));
|
|
|
|
|
|
+ Map<String, String> requestParameters = new HashMap<>();
|
|
|
|
+ requestParameters.put("request_parameters", gson.toJson(parameters));
|
|
|
|
+ requestParameters.put("request_body", body);
|
|
|
|
+ RequestLogPo requestLogPo = new RequestLogPo();
|
|
|
|
+ requestLogPo.setRequestTime(now);
|
|
|
|
+ requestLogPo.setLoginId(map.get("LOGIN_ID"));
|
|
|
|
+ requestLogPo.setPageUrl(map.get("REQUEST_URL"));
|
|
|
|
+ requestLogPo.setApi(request.getRequestURI());
|
|
|
|
+ requestLogPo.setRequestParameters(gson.toJson(requestParameters));
|
|
|
|
+ requestLogPo.setHeaders(gson.toJson(headers));
|
|
|
|
+ requestLogPo.setAppId(map.get("APP_ID"));
|
|
|
|
+ setTimeStamp(map, requestLogPo);
|
|
|
|
+ requestLogPo.setToken(urlDecodeToken);
|
|
|
|
+ requestLogService.offer(requestLogPo);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.warn("token解密失败: {}", token);
|
|
|
|
|
|
+ log.error("token解密失败: {} {}", token, e.getMessage(), e);
|
|
// return forbidden(response);
|
|
// return forbidden(response);
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void setTimeStamp(Map<String, String> map, RequestLogPo requestLogPo) {
|
|
|
|
+ String timeStamp = map.get("TIME_STAMP");
|
|
|
|
+ if (StringUtils.hasText(timeStamp)) {
|
|
|
|
+ try {
|
|
|
|
+ requestLogPo.setTimeStamp(LocalDateTime.parse(map.get("TIME_STAMP"),
|
|
|
|
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("时间戳解析失败: {}", timeStamp);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
@Nullable Exception ex) throws Exception {
|
|
@Nullable Exception ex) throws Exception {
|