AbstractRequestLoggingFilter supports logging of request headers
Issue: SPR-14245
This commit is contained in:
@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.ContentCachingRequestWrapper;
|
||||
@@ -46,7 +47,7 @@ import org.springframework.web.util.WebUtils;
|
||||
*
|
||||
* <p>Prefixes and suffixes for the before and after messages can be configured using the
|
||||
* {@code beforeMessagePrefix}, {@code afterMessagePrefix}, {@code beforeMessageSuffix} and
|
||||
* {@code afterMessageSuffix} properties,
|
||||
* {@code afterMessageSuffix} properties.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
@@ -72,6 +73,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
||||
|
||||
private boolean includeClientInfo = false;
|
||||
|
||||
private boolean includeHeaders = false;
|
||||
|
||||
private boolean includePayload = false;
|
||||
|
||||
private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
|
||||
@@ -119,12 +122,29 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
||||
return this.includeClientInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the request headers should be included in the log message.
|
||||
* <p>Should be configured using an {@code <init-param>} for parameter name
|
||||
* "includeHeaders" in the filter definition in {@code web.xml}.
|
||||
* @since 4.3
|
||||
*/
|
||||
public void setIncludeHeaders(boolean includeHeaders) {
|
||||
this.includeHeaders = includeHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the request headers should be included in the log message.
|
||||
* @since 4.3
|
||||
*/
|
||||
public boolean isIncludeHeaders() {
|
||||
return this.includeHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the request payload (body) should be included in the log message.
|
||||
* <p>Should be configured using an {@code <init-param>} for parameter name
|
||||
* "includePayload" in the filter definition in {@code web.xml}.
|
||||
*/
|
||||
|
||||
public void setIncludePayload(boolean includePayload) {
|
||||
this.includePayload = includePayload;
|
||||
}
|
||||
@@ -254,12 +274,14 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append(prefix);
|
||||
msg.append("uri=").append(request.getRequestURI());
|
||||
|
||||
if (isIncludeQueryString()) {
|
||||
String queryString = request.getQueryString();
|
||||
if (queryString != null) {
|
||||
msg.append('?').append(queryString);
|
||||
}
|
||||
}
|
||||
|
||||
if (isIncludeClientInfo()) {
|
||||
String client = request.getRemoteAddr();
|
||||
if (StringUtils.hasLength(client)) {
|
||||
@@ -274,6 +296,11 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
||||
msg.append(";user=").append(user);
|
||||
}
|
||||
}
|
||||
|
||||
if (isIncludeHeaders()) {
|
||||
msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
|
||||
}
|
||||
|
||||
if (isIncludePayload()) {
|
||||
ContentCachingRequestWrapper wrapper =
|
||||
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
|
||||
@@ -292,6 +319,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
msg.append(suffix);
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user