-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Open
Labels
in: testAn issue in spring-security-testAn issue in spring-security-testtype: bugA general bugA general bug
Milestone
Description
This PR introduced the SecurityMockMvcResultHandlers
with the exportTestSecurityContext
method. It works well when using with @WithSecurityContext
and the annotation that inherits it.
However, it does not work when not using any of those annotations. In that scenarios, if the Authentication
is null inside the TestSecurityContextHolder.getContext()
, we should consider looking into the SecurityContextRepository
. Something like this:
private static class ExportTestSecurityContextHandler implements ResultHandler {
@Override
public void handle(MvcResult result) {
SecurityContext securityContext = TestSecurityContextHolder.getContext();
if (securityContext.getAuthentication() == null) {
SecurityContextRepository securityContextRepository = WebTestUtils.getSecurityContextRepository(result.getRequest());
securityContext = securityContextRepository.loadContext(new HttpRequestResponseHolder(result.getRequest(), result.getResponse()));
}
SecurityContextHolder.setContext(securityContext);
}
}
Kehrlann
Metadata
Metadata
Assignees
Labels
in: testAn issue in spring-security-testAn issue in spring-security-testtype: bugA general bugA general bug