HttpSessionSecurityContextRepository support null HttpServletResponse

Closes gh-11029
This commit is contained in:
Rob Winch 2022-03-25 13:01:40 -05:00
parent d4d6ddbaae
commit 8940719dbb
2 changed files with 14 additions and 4 deletions

View File

@ -123,10 +123,12 @@ public class HttpSessionSecurityContextRepository implements SecurityContextRepo
this.logger.trace(LogMessage.format("Created %s", context)); this.logger.trace(LogMessage.format("Created %s", context));
} }
} }
if (response != null) {
SaveToSessionResponseWrapper wrappedResponse = new SaveToSessionResponseWrapper(response, request, SaveToSessionResponseWrapper wrappedResponse = new SaveToSessionResponseWrapper(response, request,
httpSession != null, context); httpSession != null, context);
requestResponseHolder.setResponse(wrappedResponse); requestResponseHolder.setResponse(wrappedResponse);
requestResponseHolder.setRequest(new SaveToSessionRequestWrapper(request, wrappedResponse)); requestResponseHolder.setRequest(new SaveToSessionRequestWrapper(request, wrappedResponse));
}
return context; return context;
} }

View File

@ -134,6 +134,14 @@ public class HttpSessionSecurityContextRepositoryTests {
assertThat(request.getSession(false)).isNull(); assertThat(request.getSession(false)).isNull();
} }
@Test
public void loadContextWhenNullResponse() {
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
MockHttpServletRequest request = new MockHttpServletRequest();
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, null);
assertThat(repo.loadContext(holder)).isEqualTo(SecurityContextHolder.createEmptyContext());
}
@Test @Test
public void existingContextIsSuccessFullyLoadedFromSessionAndSavedBack() { public void existingContextIsSuccessFullyLoadedFromSessionAndSavedBack() {
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository(); HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();