From 9983d6661798d5042a94a5f8b7962a5e81028ea6 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Apr 2024 16:00:47 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20AccessLog=20=E7=9A=84=20body=20?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E7=9A=84=E6=9C=80=E5=A4=A7=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E4=B8=8B=E6=94=BE=E5=88=B0=E8=A7=84=E5=88=99?= =?UTF-8?q?=E4=B8=AD=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../accesslog/AccessLogAutoConfiguration.java | 2 -- .../web/accesslog/AccessLogProperties.java | 17 +++++++--- .../accesslog/AccessLogTestConfiguration.java | 1 - .../web/accesslog/TestAccessLogFilter.java | 4 +-- .../accesslog/AbstractAccessLogFilter.java | 34 ++++++++----------- .../web/accesslog/AccessLogRecordOptions.java | 10 ++++++ .../web/accesslog/DefaultAccessLogFilter.java | 4 +-- .../annotation/AccessLogRuleFinder.java | 2 ++ .../annotation/AccessLoggingRule.java | 12 +++++++ 9 files changed, 55 insertions(+), 31 deletions(-) diff --git a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogAutoConfiguration.java b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogAutoConfiguration.java index d8ebb0203..647f2b7f7 100644 --- a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogAutoConfiguration.java +++ b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogAutoConfiguration.java @@ -56,7 +56,6 @@ public class AccessLogAutoConfiguration { @ConditionalOnMissingBean(AccessLogFilter.class) public AccessLogFilter defaultAccessLogFilter() { List propertiesRules = this.accessLogProperties.getAccessLogRules(); - Integer maxBodyLength = this.accessLogProperties.getMaxBodyLength(); Integer filterOrder = this.accessLogProperties.getFilterOrder(); Level defaultFilterLogLevel = this.accessLogProperties.getDefaultFilterLogLevel(); AccessLogRecordOptions defaultRecordOptions = this.accessLogProperties.getDefaultAccessLogRecordOptions(); @@ -69,7 +68,6 @@ public AccessLogFilter defaultAccessLogFilter() { // 创建默认访问日志过滤器 AbstractAccessLogFilter accessLogFilter = new DefaultAccessLogFilter(defaultRecordOptions, accessLogRules, defaultFilterLogLevel); - accessLogFilter.setMaxBodyLength(maxBodyLength); accessLogFilter.setOrder(filterOrder); return accessLogFilter; } diff --git a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogProperties.java b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogProperties.java index 96012c4a1..810102a3e 100644 --- a/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogProperties.java +++ b/web/ballcat-spring-boot-starter-web/src/main/java/org/ballcat/autoconfigure/web/accesslog/AccessLogProperties.java @@ -62,11 +62,6 @@ public class AccessLogProperties { */ private Boolean filterAutoRegister = true; - /** - * 记录的最大的 body 长度 - */ - private Integer maxBodyLength = AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH; - /** * 访问日志记录的默认选项,当请求路径无法在 rules 中匹配时,使用该选项 */ @@ -93,6 +88,8 @@ private static AccessLogRecordOptions convertToAccessLogRecordOptions(RecordOpti .includeQueryString(recordOptions.isIncludeQueryString()) .includeRequestBody(recordOptions.isIncludeRequestBody()) .includeResponseBody(recordOptions.isIncludeResponseBody()) + .maxRequestBodyLength(recordOptions.getMaxRequestBodyLength()) + .maxResponseBodyLength(recordOptions.getMaxResponseBodyLength()) .build(); } @@ -124,6 +121,16 @@ static class RecordOptions { */ private boolean includeResponseBody = false; + /** + * 记录的最大的请求 body 长度 + */ + private Integer maxRequestBodyLength = AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH; + + /** + * 记录的最大的响应 body 长度 + */ + private Integer maxResponseBodyLength = AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH; + } @Data diff --git a/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/AccessLogTestConfiguration.java b/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/AccessLogTestConfiguration.java index 6728893b9..562576f4e 100644 --- a/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/AccessLogTestConfiguration.java +++ b/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/AccessLogTestConfiguration.java @@ -57,7 +57,6 @@ public AccessLogFilter accessLogFilter() { AccessLogRecordOptions defaultRecordOptions = this.accessLogProperties.getDefaultAccessLogRecordOptions(); TestAccessLogFilter accessLogFilter = new TestAccessLogFilter(defaultRecordOptions, accessLogRules); - accessLogFilter.setMaxBodyLength(this.accessLogProperties.getMaxBodyLength()); accessLogFilter.setOrder(this.accessLogProperties.getFilterOrder()); return accessLogFilter; } diff --git a/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/TestAccessLogFilter.java b/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/TestAccessLogFilter.java index 0cc2e9957..52bee53d5 100644 --- a/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/TestAccessLogFilter.java +++ b/web/ballcat-spring-boot-starter-web/src/test/java/org/ballcat/autoconfigure/web/accesslog/TestAccessLogFilter.java @@ -47,14 +47,14 @@ protected void beforeRequest(HttpServletRequest request, AccessLogRecordOptions protected void afterRequest(HttpServletRequest request, HttpServletResponse response, Long executionTime, Throwable throwable, AccessLogRecordOptions recordOptions) { if (recordOptions.isIncludeRequestBody()) { - String payload = getRequestBody(request); + String payload = getRequestBody(request, recordOptions.getMaxRequestBodyLength()); if (payload != null) { this.httpInfo.setRequestBody(payload); } } if (recordOptions.isIncludeResponseBody()) { - String payload = getResponseBody(response); + String payload = getResponseBody(response, recordOptions.getMaxResponseBodyLength()); if (payload != null) { this.httpInfo.setResponseBody(payload); } diff --git a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AbstractAccessLogFilter.java b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AbstractAccessLogFilter.java index 769929c5b..b8548a9b9 100644 --- a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AbstractAccessLogFilter.java +++ b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AbstractAccessLogFilter.java @@ -58,8 +58,6 @@ public abstract class AbstractAccessLogFilter extends OncePerRequestFilter imple public static final int DEFAULT_MAX_BODY_LENGTH = 256; - private int maxBodyLength = DEFAULT_MAX_BODY_LENGTH; - private int order = 0; private final AccessLogRecordOptions defaultRecordOptions; @@ -180,35 +178,42 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } @Nullable - protected String getRequestBody(HttpServletRequest request) { + protected String getRequestBody(HttpServletRequest request, int maxLength) { RepeatBodyRequestWrapper wrapper = WebUtils.getNativeRequest(request, RepeatBodyRequestWrapper.class); if (wrapper == null) { return null; } if (wrapper.getCharacterEncoding() != null) { - return getMessagePayload(wrapper.getBodyByteArray(), wrapper.getCharacterEncoding()); + return getMessagePayload(wrapper.getBodyByteArray(), maxLength, wrapper.getCharacterEncoding()); } else { - return getMessagePayload(wrapper.getBodyByteArray(), Charset.defaultCharset().name()); + return getMessagePayload(wrapper.getBodyByteArray(), maxLength, Charset.defaultCharset().name()); } } @Nullable - protected String getResponseBody(HttpServletResponse response) { + protected String getResponseBody(HttpServletResponse response, int maxLength) { ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); if (wrapper == null) { return null; } - return getMessagePayload(wrapper.getContentAsByteArray(), Charset.defaultCharset().name()); + return getMessagePayload(wrapper.getContentAsByteArray(), maxLength, Charset.defaultCharset().name()); } @Nullable - protected String getMessagePayload(byte[] buf, String characterEncoding) { + protected String getMessagePayload(byte[] buf, int maxLength, String characterEncoding) { if (buf.length > 0) { - int length = Math.min(buf.length, getMaxBodyLength()); try { - return new String(buf, 0, length, characterEncoding); + if (maxLength < 0) { + return new String(buf, characterEncoding); + } + else if (maxLength == 0) { + return ""; + } + else { + return new String(buf, 0, Math.min(buf.length, maxLength), characterEncoding); + } } catch (UnsupportedEncodingException ex) { return "[unknown]"; @@ -233,11 +238,6 @@ protected AccessLogRecordOptions getRecordOptions(HttpServletRequest request) { return this.defaultRecordOptions; } - public void setMaxBodyLength(int maxBodyLength) { - Assert.isTrue(maxBodyLength >= 0, "'maxBodyLength' must be greater than or equal to 0"); - this.maxBodyLength = maxBodyLength; - } - public void setOrder(int order) { this.order = order; } @@ -247,10 +247,6 @@ public int getOrder() { return this.order; } - protected int getMaxBodyLength() { - return this.maxBodyLength; - } - protected boolean shouldLog(HttpServletRequest request) { return true; } diff --git a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AccessLogRecordOptions.java b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AccessLogRecordOptions.java index 0cdf7c6b8..5ac3125ed 100644 --- a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AccessLogRecordOptions.java +++ b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/AccessLogRecordOptions.java @@ -49,4 +49,14 @@ public class AccessLogRecordOptions { */ private boolean includeResponseBody; + /** + * 记录的最大的请求 body 长度 + */ + private int maxRequestBodyLength; + + /** + * 记录的最大的响应 body 长度 + */ + private int maxResponseBodyLength; + } diff --git a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/DefaultAccessLogFilter.java b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/DefaultAccessLogFilter.java index e147d7b38..809f5fddb 100644 --- a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/DefaultAccessLogFilter.java +++ b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/DefaultAccessLogFilter.java @@ -104,14 +104,14 @@ protected void afterRequest(HttpServletRequest request, HttpServletResponse resp msg.append(", client=").append(ipAddr); if (recordOptions.isIncludeRequestBody()) { - String payload = getRequestBody(request); + String payload = getRequestBody(request, recordOptions.getMaxRequestBodyLength()); if (payload != null) { msg.append(", request body=").append(payload); } } if (recordOptions.isIncludeResponseBody()) { - String payload = getResponseBody(response); + String payload = getResponseBody(response, recordOptions.getMaxResponseBodyLength()); if (payload != null) { msg.append(", response body=").append(payload); } diff --git a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLogRuleFinder.java b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLogRuleFinder.java index bfe50b0e1..e5185e4fd 100644 --- a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLogRuleFinder.java +++ b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLogRuleFinder.java @@ -81,6 +81,8 @@ public static AccessLogRecordOptions convertToRecordOptions(AccessLoggingRule ac .includeQueryString(accessLoggingRule.includeQueryString()) .includeRequestBody(accessLoggingRule.includeRequestBody()) .includeResponseBody(accessLoggingRule.includeResponseBody()) + .maxRequestBodyLength(accessLoggingRule.maxRequestBodyLength()) + .maxResponseBodyLength(accessLoggingRule.maxResponseBodyLength()) .build(); } diff --git a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLoggingRule.java b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLoggingRule.java index a939b502d..9f7e51559 100644 --- a/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLoggingRule.java +++ b/web/ballcat-web/src/main/java/org/ballcat/web/accesslog/annotation/AccessLoggingRule.java @@ -22,6 +22,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.ballcat.web.accesslog.AbstractAccessLogFilter; + /** * @author Alickx 2023/11/23 17:48 * @since 2.0.0 @@ -51,4 +53,14 @@ */ boolean includeResponseBody() default false; + /** + * 记录的最大的请求 body 长度 + */ + int maxRequestBodyLength() default AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH; + + /** + * 记录的最大的响应 body 长度 + */ + int maxResponseBodyLength() default AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH; + }