From 6aebedee20f028bab732befc4cf6ae1780b3bf07 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期四, 05 二月 2026 15:15:17 +0800
Subject: [PATCH] #
---
zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationOpenApiInterceptor.java | 84 ++------------------
zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationRecordAdvice.java | 99 ++++++++++++++++++++++++
2 files changed, 106 insertions(+), 77 deletions(-)
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationOpenApiInterceptor.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationOpenApiInterceptor.java
index 035356b..6abff13 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationOpenApiInterceptor.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationOpenApiInterceptor.java
@@ -1,24 +1,16 @@
package com.zy.acs.manager.common.interceptor;
-import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.annotation.IntegrationAuth;
import com.zy.acs.manager.core.domain.type.NamespaceType;
-import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
-import org.springframework.web.util.ContentCachingRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.Map;
/**
* Intercepts open API calls so they can be audited via IntegrationRecordAdvice.
@@ -44,8 +36,7 @@
IntegrationRequestContext context = new IntegrationRequestContext(
namespaceType,
handlerMethod.getBeanType().getSimpleName() + "#" + handlerMethod.getMethod().getName(),
- System.currentTimeMillis(),
- buildPayload(request)
+ System.currentTimeMillis()
);
request.setAttribute(ATTR_CONTEXT, context);
return true;
@@ -67,76 +58,19 @@
return AnnotatedElementUtils.findMergedAnnotation(handlerMethod.getBeanType(), IntegrationAuth.class);
}
- private RequestPayload buildPayload(HttpServletRequest request) {
- Map<String, Object> params = flattenParameters(request.getParameterMap());
- String body = readBody(request);
- return new RequestPayload(
- request.getMethod(),
- request.getRequestURI(),
- request.getQueryString(),
- request.getContentType(),
- params.isEmpty() ? null : params,
- Cools.isEmpty(body) ? null : body
- );
- }
-
- private Map<String, Object> flattenParameters(Map<String, String[]> rawParams) {
- Map<String, Object> flattened = new LinkedHashMap<>();
- if (rawParams == null) {
- return flattened;
- }
- rawParams.forEach((key, values) -> {
- if (values == null) {
- flattened.put(key, null);
- } else if (values.length == 1) {
- flattened.put(key, values[0]);
- } else {
- flattened.put(key, Arrays.asList(values));
- }
- });
- return flattened;
- }
-
- private String readBody(HttpServletRequest request) {
- if (request instanceof ContentCachingRequestWrapper) {
- ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
- byte[] buffer = wrapper.getContentAsByteArray();
- if (buffer.length > 0) {
- Charset charset = charset(wrapper.getCharacterEncoding());
- return new String(buffer, charset);
- }
- }
- return null;
- }
-
- private Charset charset(String encoding) {
- if (Cools.isEmpty(encoding)) {
- return StandardCharsets.UTF_8;
- }
- try {
- return Charset.forName(encoding);
- } catch (Exception e) {
- return StandardCharsets.UTF_8;
- }
- }
-
@Getter
- @AllArgsConstructor
public static class IntegrationRequestContext {
+
+ private IntegrationRequestContext(NamespaceType namespaceType, String handler, long startAt) {
+ this.namespaceType = namespaceType;
+ this.handler = handler;
+ this.startAt = startAt;
+ }
+
private final NamespaceType namespaceType;
private final String handler;
private final long startAt;
- private final RequestPayload payload;
+
}
- @Getter
- @AllArgsConstructor
- public static class RequestPayload {
- private final String method;
- private final String uri;
- private final String query;
- private final String contentType;
- private final Map<String, Object> parameters;
- private final String body;
- }
}
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationRecordAdvice.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationRecordAdvice.java
index 852a20b..9ffd4d9 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationRecordAdvice.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/common/interceptor/IntegrationRecordAdvice.java
@@ -5,7 +5,6 @@
import com.zy.acs.framework.common.R;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.manager.common.interceptor.IntegrationOpenApiInterceptor.IntegrationRequestContext;
-import com.zy.acs.manager.common.interceptor.IntegrationOpenApiInterceptor.RequestPayload;
import com.zy.acs.manager.common.utils.IpTools;
import com.zy.acs.manager.manager.entity.IntegrationRecord;
import com.zy.acs.manager.manager.enums.IntegrationDirectionType;
@@ -20,12 +19,19 @@
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
+import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
import java.util.UUID;
@Slf4j
@@ -84,7 +90,9 @@
IntegrationRequestContext context,
Exception failure) {
Date now = new Date();
- RequestPayload payload = context.getPayload();
+ RequestSnapshot payload = buildSnapshot(request);
+
+ System.out.println(JSON.toJSONString(payload.body));
IntegrationRecord record = new IntegrationRecord();
record.setUuid(nextUuid());
@@ -181,4 +189,91 @@
private String safeToString(Object value) {
return value == null ? null : String.valueOf(value);
}
+ private RequestSnapshot buildSnapshot(HttpServletRequest request) {
+ Map<String, Object> params = flattenParameters(request.getParameterMap());
+ String body = readBody(request);
+ return new RequestSnapshot(
+ request.getMethod(),
+ request.getRequestURI(),
+ request.getQueryString(),
+ request.getContentType(),
+ params.isEmpty() ? null : params,
+ body
+ );
+ }
+
+ private Map<String, Object> flattenParameters(Map<String, String[]> rawParams) {
+ Map<String, Object> flattened = new LinkedHashMap<>();
+ if (rawParams == null) {
+ return flattened;
+ }
+ rawParams.forEach((key, values) -> {
+ if (values == null) {
+ flattened.put(key, null);
+ } else if (values.length == 1) {
+ flattened.put(key, values[0]);
+ } else {
+ flattened.put(key, Arrays.asList(values));
+ }
+ });
+ return flattened;
+ }
+
+ private String readBody(HttpServletRequest request) {
+ HttpServletRequest target = unwrapCachingRequest(request);
+ if (target instanceof ContentCachingRequestWrapper) {
+ ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) target;
+ byte[] buffer = wrapper.getContentAsByteArray();
+ if (buffer.length > 0) {
+ Charset charset = charset(wrapper.getCharacterEncoding());
+ return new String(buffer, charset);
+ }
+ }
+ return null;
+ }
+
+ private HttpServletRequest unwrapCachingRequest(HttpServletRequest request) {
+ HttpServletRequest current = request;
+ while (current instanceof HttpServletRequestWrapper) {
+ if (current instanceof ContentCachingRequestWrapper) {
+ return current;
+ }
+ current = (HttpServletRequest) ((HttpServletRequestWrapper) current).getRequest();
+ }
+ return request;
+ }
+
+ private Charset charset(String encoding) {
+ if (Cools.isEmpty(encoding)) {
+ return StandardCharsets.UTF_8;
+ }
+ try {
+ return Charset.forName(encoding);
+ } catch (Exception e) {
+ return StandardCharsets.UTF_8;
+ }
+ }
+
+ private static class RequestSnapshot {
+ private final String method;
+ private final String uri;
+ private final String query;
+ private final String contentType;
+ private final Map<String, Object> parameters;
+ private final String body;
+
+ RequestSnapshot(String method, String uri, String query, String contentType,
+ Map<String, Object> parameters, String body) {
+ this.method = method;
+ this.uri = uri;
+ this.query = query;
+ this.contentType = contentType;
+ this.parameters = parameters;
+ this.body = body;
+ }
+
+ public String getUri() {
+ return uri;
+ }
+ }
}
--
Gitblit v1.9.1