|
@@ -1,6 +1,10 @@
|
|
package com.nokia.financeapi.config.web;
|
|
package com.nokia.financeapi.config.web;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
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.utils.AESUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.slf4j.MDC;
|
|
import org.slf4j.MDC;
|
|
import org.springframework.lang.Nullable;
|
|
import org.springframework.lang.Nullable;
|
|
@@ -11,7 +15,9 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
|
+import java.time.LocalDateTime;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -35,6 +41,7 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
STOP_WATCH_THREAD_LOCAL.set(stopWatch);
|
|
STOP_WATCH_THREAD_LOCAL.set(stopWatch);
|
|
// 日志添加跟踪id
|
|
// 日志添加跟踪id
|
|
MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
|
|
MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
log.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
|
|
log.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
|
|
// 请求头参数
|
|
// 请求头参数
|
|
Map<String, String> headers = new HashMap<>();
|
|
Map<String, String> headers = new HashMap<>();
|
|
@@ -55,8 +62,35 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
// }
|
|
// }
|
|
// log.info("查询参数: {}", new ObjectMapper().writeValueAsString(parameters));
|
|
// log.info("查询参数: {}", new ObjectMapper().writeValueAsString(parameters));
|
|
// 请求体参数
|
|
// 请求体参数
|
|
- String body = StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
|
|
|
|
- log.info("请求参数: {}", StringUtils.trimAllWhitespace(body));
|
|
|
|
|
|
+ String body = StringUtils.trimAllWhitespace(StreamUtils.copyToString(request.getInputStream(),
|
|
|
|
+ Charset.forName(request.getCharacterEncoding())));
|
|
|
|
+ log.info("请求参数: {}", body);
|
|
|
|
+ // todo: 记录日志
|
|
|
|
+ String token = request.getHeader("Token");
|
|
|
|
+ log.info("token: {}", token);
|
|
|
|
+ if (!StringUtils.hasText(token)) {
|
|
|
|
+ return forbidden(response);
|
|
|
|
+ }
|
|
|
|
+ if ("test_hebei".equals(token)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ String decodeToken = AESUtil.decrypt(token);
|
|
|
|
+ log.info("decodeToken: {}", decodeToken);
|
|
|
|
+ Gson gson = new Gson();
|
|
|
|
+ Map<String, String> map = gson.fromJson(decodeToken, new TypeToken<Map<String, String>>() {
|
|
|
|
+ }.getType());
|
|
|
|
+ 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));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.warn("token解密失败: {}", token);
|
|
|
|
+ return forbidden(response);
|
|
|
|
+ }
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -74,4 +108,12 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
|
|
log.info("耗时 {} ms", stopWatch.getTotalTimeMillis());
|
|
log.info("耗时 {} ms", stopWatch.getTotalTimeMillis());
|
|
STOP_WATCH_THREAD_LOCAL.remove();
|
|
STOP_WATCH_THREAD_LOCAL.remove();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private boolean forbidden(HttpServletResponse response) throws IOException {
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
|
+ byte[] bytes = new Gson().toJson(R.error().code(403).message("没有权限访问")).getBytes();
|
|
|
|
+ response.getOutputStream().write(bytes);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|