|
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import com.nokia.financeapi.common.R;
|
|
|
+import com.nokia.financeapi.pojo.po.common.RequestLogPo;
|
|
|
+import com.nokia.financeapi.service.common.RequestLogService;
|
|
|
import com.nokia.financeapi.utils.AESUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.MDC;
|
|
@@ -20,6 +22,7 @@ import java.net.URLDecoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Enumeration;
|
|
|
import java.util.HashMap;
|
|
|
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 RequestLogService requestLogService;
|
|
|
+
|
|
|
+ public RequestLogHandlerInterceptor(RequestLogService requestLogService) {
|
|
|
+ this.requestLogService = requestLogService;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
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()));
|
|
|
log.info("请求体参数: {}", StringUtils.trimAllWhitespace(body));
|
|
|
}
|
|
|
- // todo: 记录日志
|
|
|
String token = request.getHeader("token");
|
|
|
log.info("token: {}", token);
|
|
|
if (!StringUtils.hasText(token)) {
|
|
@@ -86,10 +93,20 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
if (!"FINANCE".equals(map.get("APP_ID")) || !StringUtils.hasText(map.get("LOGIN_ID"))) {
|
|
|
return forbidden(response);
|
|
|
}
|
|
|
- map.put("api", request.getRequestURI());
|
|
|
- map.put("body", body);
|
|
|
- map.put("time", now.toString());
|
|
|
- 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) {
|
|
|
log.warn("token解密失败: {}", token);
|
|
|
return forbidden(response);
|
|
@@ -97,16 +114,28 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
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
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
@Nullable Exception ex) throws Exception {
|
|
|
if (!MyDispatcherServlet.UN_WRAPPER.contains(request.getRequestURI())) {
|
|
|
MyHttpServletResponseWrapper wrapper = (MyHttpServletResponseWrapper) response;
|
|
|
String responseString = new String(wrapper.toByteArray());
|
|
|
- log.info("响应 {}: {}", wrapper.getStatus(), responseString);
|
|
|
- // 返回结果打印前1000个字符
|
|
|
-// log.info("返回 {}: {}", wrapper.getStatus(),
|
|
|
-// org.apache.commons.lang3.StringUtils.substring(responseString, 0, 1000));
|
|
|
+// log.info("响应 {}: {}", wrapper.getStatus(), responseString);
|
|
|
+ // 返回结果打印前500个字符
|
|
|
+ log.info("返回 {}: {}", wrapper.getStatus(),
|
|
|
+ org.apache.commons.lang3.StringUtils.substring(responseString, 0, 500));
|
|
|
} else {
|
|
|
log.info("响应 {}", response.getStatus());
|
|
|
}
|