mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-26 05:42:31 +00:00
SEC-1735: Do not remove SecurityContext from HttpSession when anonymous Authentication is saved if original SecurityContext was anonymous
This commit is contained in:
parent
1f835fec43
commit
5d94cd5e13
@ -269,8 +269,9 @@ public class HttpSessionSecurityContextRepository implements SecurityContextRepo
|
|||||||
logger.debug("SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.");
|
logger.debug("SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (httpSession != null) {
|
if (httpSession != null && !contextObject.equals(contextBeforeExecution)) {
|
||||||
// SEC-1587 A non-anonymous context may still be in the session
|
// SEC-1587 A non-anonymous context may still be in the session
|
||||||
|
// SEC-1735 remove if the contextBeforeExecution was not anonymous
|
||||||
httpSession.removeAttribute(springSecurityContextKey);
|
httpSession.removeAttribute(springSecurityContextKey);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -203,6 +203,21 @@ public class HttpSessionSecurityContextRepositoryTests {
|
|||||||
assertNull(request.getSession().getAttribute("imTheContext"));
|
assertNull(request.getSession().getAttribute("imTheContext"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SEC-1735
|
||||||
|
@Test
|
||||||
|
public void contextIsNotRemovedFromSessionIfContextBeforeExecutionDefault() throws Exception {
|
||||||
|
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, new MockHttpServletResponse());
|
||||||
|
repo.loadContext(holder);
|
||||||
|
SecurityContext ctxInSession = SecurityContextHolder.createEmptyContext();
|
||||||
|
ctxInSession.setAuthentication(testToken);
|
||||||
|
request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, ctxInSession);
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("x","x", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")));
|
||||||
|
repo.saveContext(SecurityContextHolder.getContext(), holder.getRequest(), holder.getResponse());
|
||||||
|
assertSame(ctxInSession,request.getSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public void sessionDisableUrlRewritingPreventsSessionIdBeingWrittenToUrl() throws Exception {
|
public void sessionDisableUrlRewritingPreventsSessionIdBeingWrittenToUrl() throws Exception {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user