Skip to content

Commit 0fe7823

Browse files
committed
Support ForwardingHttpRequest
1 parent 0eb5286 commit 0fe7823

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main/java/am/ik/spring/logbook/ServletAwareAccessLoggerSink.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package am.ik.spring.logbook;
22

33
import jakarta.servlet.http.HttpServletRequest;
4+
import org.zalando.logbook.ForwardingHttpRequest;
45
import org.zalando.logbook.HttpRequest;
56

67
public class ServletAwareAccessLoggerSink extends AccessLoggerSink {
78

89
@Override
910
protected String getUsername(HttpRequest request) {
10-
if (request instanceof HttpServletRequest httpServletRequest) {
11+
if (request instanceof ForwardingHttpRequest forwardingHttpRequest) {
12+
return getUsername(forwardingHttpRequest.delegate());
13+
}
14+
else if (request instanceof HttpServletRequest httpServletRequest) {
1115
return httpServletRequest.getRemoteUser();
1216
}
1317
return null;

src/test/java/org/zalando/logbook/servlet/ServletAwareAccessLoggerSinkTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.mock.web.MockHttpServletRequest;
1111
import org.springframework.mock.web.MockHttpServletResponse;
1212
import org.zalando.logbook.Correlation;
13+
import org.zalando.logbook.ForwardingHttpRequest;
1314

1415
import static org.assertj.core.api.Assertions.assertThat;
1516

@@ -49,6 +50,18 @@ void shouldUsernameContainedIfRemoteUserIsNotNull(CapturedOutput capturedOutput)
4950
assertThat(capturedOutput.toString()).contains("username=\"[email protected]\"");
5051
}
5152

53+
@Test
54+
void shouldUsernameContainedIfRemoteUserIsNotNullForForwardingRequest(CapturedOutput capturedOutput)
55+
throws Exception {
56+
ServletAwareAccessLoggerSink sink = new ServletAwareAccessLoggerSink();
57+
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
58+
mockRequest.setRemoteUser("[email protected]");
59+
RemoteRequest request = new RemoteRequest(mockRequest, FormRequestMode.OFF);
60+
LocalResponse response = new LocalResponse(new MockHttpServletResponse(), "2.0");
61+
sink.write(correlation, (ForwardingHttpRequest) () -> request, response);
62+
assertThat(capturedOutput.toString()).contains("username=\"[email protected]\"");
63+
}
64+
5265
@Test
5366
void shouldNotUsernameContainedIfRemoteUserIsNull(CapturedOutput capturedOutput) throws Exception {
5467
ServletAwareAccessLoggerSink sink = new ServletAwareAccessLoggerSink();
@@ -59,4 +72,15 @@ void shouldNotUsernameContainedIfRemoteUserIsNull(CapturedOutput capturedOutput)
5972
assertThat(capturedOutput.toString()).doesNotContain("username=");
6073
}
6174

75+
@Test
76+
void shouldNotUsernameContainedIfRemoteUserIsNullForFowardingRequest(CapturedOutput capturedOutput)
77+
throws Exception {
78+
ServletAwareAccessLoggerSink sink = new ServletAwareAccessLoggerSink();
79+
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
80+
RemoteRequest request = new RemoteRequest(mockRequest, FormRequestMode.OFF);
81+
LocalResponse response = new LocalResponse(new MockHttpServletResponse(), "2.0");
82+
sink.write(correlation, (ForwardingHttpRequest) () -> request, response);
83+
assertThat(capturedOutput.toString()).doesNotContain("username=");
84+
}
85+
6286
}

0 commit comments

Comments
 (0)