Skip to content

Commit

Permalink
✨ AccessLog 的 body 记录的最大长度控制下放到规则中控制
Browse files Browse the repository at this point in the history
  • Loading branch information
evil0th authored and Hccake committed Apr 10, 2024
1 parent f706fc5 commit 9983d66
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class AccessLogAutoConfiguration {
@ConditionalOnMissingBean(AccessLogFilter.class)
public AccessLogFilter defaultAccessLogFilter() {
List<AccessLogRule> propertiesRules = this.accessLogProperties.getAccessLogRules();
Integer maxBodyLength = this.accessLogProperties.getMaxBodyLength();
Integer filterOrder = this.accessLogProperties.getFilterOrder();
Level defaultFilterLogLevel = this.accessLogProperties.getDefaultFilterLogLevel();
AccessLogRecordOptions defaultRecordOptions = this.accessLogProperties.getDefaultAccessLogRecordOptions();
Expand All @@ -69,7 +68,6 @@ public AccessLogFilter defaultAccessLogFilter() {
// 创建默认访问日志过滤器
AbstractAccessLogFilter accessLogFilter = new DefaultAccessLogFilter(defaultRecordOptions, accessLogRules,
defaultFilterLogLevel);
accessLogFilter.setMaxBodyLength(maxBodyLength);
accessLogFilter.setOrder(filterOrder);
return accessLogFilter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public class AccessLogProperties {
*/
private Boolean filterAutoRegister = true;

/**
* 记录的最大的 body 长度
*/
private Integer maxBodyLength = AbstractAccessLogFilter.DEFAULT_MAX_BODY_LENGTH;

/**
* 访问日志记录的默认选项,当请求路径无法在 rules 中匹配时,使用该选项
*/
Expand All @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]";
Expand All @@ -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;
}
Expand All @@ -247,10 +247,6 @@ public int getOrder() {
return this.order;
}

protected int getMaxBodyLength() {
return this.maxBodyLength;
}

protected boolean shouldLog(HttpServletRequest request) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public class AccessLogRecordOptions {
*/
private boolean includeResponseBody;

/**
* 记录的最大的请求 body 长度
*/
private int maxRequestBodyLength;

/**
* 记录的最大的响应 body 长度
*/
private int maxResponseBodyLength;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

}

0 comments on commit 9983d66

Please sign in to comment.