Skip to content

Commit 66c4452

Browse files
committed
AbstractRequestLoggingFilter unwraps request to find ContentCachingRequestWrapper
Issue: SPR-13764 (cherry picked from commit b1ef6ec)
1 parent 0230c49 commit 66c4452

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.util.Assert;
2828
import org.springframework.util.StringUtils;
2929
import org.springframework.web.util.ContentCachingRequestWrapper;
30+
import org.springframework.web.util.WebUtils;
3031

3132
/**
3233
* Base class for {@code Filter}s that perform logging operations before and after a request
@@ -86,7 +87,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
8687

8788
/**
8889
* Set whether the query string should be included in the log message.
89-
* <p>Should be configured using an {@code &lt;init-param&gt;} for parameter name
90+
* <p>Should be configured using an {@code <init-param>} for parameter name
9091
* "includeQueryString" in the filter definition in {@code web.xml}.
9192
*/
9293
public void setIncludeQueryString(boolean includeQueryString) {
@@ -103,7 +104,7 @@ protected boolean isIncludeQueryString() {
103104
/**
104105
* Set whether the client address and session id should be included in the
105106
* log message.
106-
* <p>Should be configured using an {@code &lt;init-param&gt;} for parameter name
107+
* <p>Should be configured using an {@code <init-param>} for parameter name
107108
* "includeClientInfo" in the filter definition in {@code web.xml}.
108109
*/
109110
public void setIncludeClientInfo(boolean includeClientInfo) {
@@ -120,7 +121,7 @@ protected boolean isIncludeClientInfo() {
120121

121122
/**
122123
* Set whether the request payload (body) should be included in the log message.
123-
* <p>Should be configured using an {@code &lt;init-param&gt;} for parameter name
124+
* <p>Should be configured using an {@code <init-param>} for parameter name
124125
* "includePayload" in the filter definition in {@code web.xml}.
125126
*/
126127

@@ -270,21 +271,23 @@ protected String createMessage(HttpServletRequest request, String prefix, String
270271
msg.append(";user=").append(user);
271272
}
272273
}
273-
if (isIncludePayload() && request instanceof ContentCachingRequestWrapper) {
274-
ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
275-
byte[] buf = wrapper.getContentAsByteArray();
276-
if (buf.length > 0) {
277-
int length = Math.min(buf.length, getMaxPayloadLength());
278-
String payload;
279-
try {
280-
payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
274+
if (isIncludePayload()) {
275+
ContentCachingRequestWrapper wrapper =
276+
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
277+
if (wrapper != null) {
278+
byte[] buf = wrapper.getContentAsByteArray();
279+
if (buf.length > 0) {
280+
int length = Math.min(buf.length, getMaxPayloadLength());
281+
String payload;
282+
try {
283+
payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
284+
}
285+
catch (UnsupportedEncodingException ex) {
286+
payload = "[unknown]";
287+
}
288+
msg.append(";payload=").append(payload);
281289
}
282-
catch (UnsupportedEncodingException e) {
283-
payload = "[unknown]";
284-
}
285-
msg.append(";payload=").append(payload);
286290
}
287-
288291
}
289292
msg.append(suffix);
290293
return msg.toString();

0 commit comments

Comments
 (0)