|
@@ -1,17 +1,26 @@
|
|
|
package com.nokia.finance.tasks.config.web;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.MDC;
|
|
|
+import org.springframework.core.MethodParameter;
|
|
|
+import org.springframework.http.HttpInputMessage;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.converter.HttpMessageConverter;
|
|
|
+import org.springframework.http.server.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.ServerHttpResponse;
|
|
|
import org.springframework.lang.Nullable;
|
|
|
import org.springframework.util.StopWatch;
|
|
|
-import org.springframework.util.StreamUtils;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.nio.charset.Charset;
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Type;
|
|
|
import java.util.Enumeration;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -21,11 +30,14 @@ import java.util.UUID;
|
|
|
* 请求日志拦截器
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
+@ControllerAdvice
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class RequestLogHandlerInterceptor implements HandlerInterceptor, RequestBodyAdvice, ResponseBodyAdvice<Object> {
|
|
|
/**
|
|
|
* 计时器线程变量
|
|
|
*/
|
|
|
private static final ThreadLocal<StopWatch> STOP_WATCH_THREAD_LOCAL = new ThreadLocal<>();
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
@@ -44,7 +56,7 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
String v = request.getHeader(k);
|
|
|
headers.put(k, v);
|
|
|
}
|
|
|
- log.info("请求头参数: {}", new ObjectMapper().writeValueAsString(headers));
|
|
|
+ log.info("请求头参数: {}", objectMapper.writeValueAsString(headers));
|
|
|
// 查询参数
|
|
|
Map<String, String> parameters = new HashMap<>();
|
|
|
Enumeration<String> parameterNames = request.getParameterNames();
|
|
@@ -53,10 +65,7 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
String v = request.getParameter(k);
|
|
|
parameters.put(k, v);
|
|
|
}
|
|
|
- log.info("查询参数: {}", new ObjectMapper().writeValueAsString(parameters));
|
|
|
- // 请求体参数
|
|
|
- String body = StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
|
|
|
- log.info("请求体参数: {}", StringUtils.trimAllWhitespace(body));
|
|
|
+ log.info("查询参数: {}", objectMapper.writeValueAsString(parameters));
|
|
|
String remoteAddr = request.getRemoteAddr();
|
|
|
// 判断请求是否来自本地
|
|
|
if (!"127.0.0.1".equals(remoteAddr)) {
|
|
@@ -69,12 +78,54 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
|
@Override
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
@Nullable Exception ex) throws Exception {
|
|
|
- MyHttpServletResponseWrapper wrapper = (MyHttpServletResponseWrapper) response;
|
|
|
- String responseString = new String(wrapper.toByteArray());
|
|
|
- log.info("响应 {}: {}", wrapper.getStatus(), responseString);
|
|
|
+ log.info("响应状态: {}", response.getStatus());
|
|
|
StopWatch stopWatch = STOP_WATCH_THREAD_LOCAL.get();
|
|
|
stopWatch.stop();
|
|
|
- log.info("耗时 {} ms", stopWatch.getTotalTimeMillis());
|
|
|
+ log.info("耗时: {} ms", stopWatch.getTotalTimeMillis());
|
|
|
STOP_WATCH_THREAD_LOCAL.remove();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
|
|
+ return inputMessage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
+ try {
|
|
|
+ if (STOP_WATCH_THREAD_LOCAL.get() != null) {
|
|
|
+ log.info("请求体参数: {}", objectMapper.writeValueAsString(body));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString(), e);
|
|
|
+ }
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
|
|
+ try {
|
|
|
+ if (STOP_WATCH_THREAD_LOCAL.get() != null) {
|
|
|
+ log.info("响应内容: {}", org.apache.commons.lang3.StringUtils.substring(objectMapper.writeValueAsString(body), 0, 500));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString(), e);
|
|
|
+ }
|
|
|
+ return body;
|
|
|
+ }
|
|
|
}
|