Skip to content

Commit cbba7ea

Browse files
committed
AbstractAuthenticationProcessingFilter.securityContextRepository
Issue gh-10953
1 parent ae7d56d commit cbba7ea

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.springframework.security.core.context.SecurityContextHolder;
4343
import org.springframework.security.web.authentication.session.NullAuthenticatedSessionStrategy;
4444
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
45+
import org.springframework.security.web.context.NullSecurityContextRepository;
46+
import org.springframework.security.web.context.SecurityContextRepository;
4547
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
4648
import org.springframework.security.web.util.matcher.RequestMatcher;
4749
import org.springframework.util.Assert;
@@ -134,6 +136,8 @@ public abstract class AbstractAuthenticationProcessingFilter extends GenericFilt
134136

135137
private AuthenticationFailureHandler failureHandler = new SimpleUrlAuthenticationFailureHandler();
136138

139+
private SecurityContextRepository securityContextRepository = new NullSecurityContextRepository();
140+
137141
/**
138142
* @param defaultFilterProcessesUrl the default value for <tt>filterProcessesUrl</tt>.
139143
*/
@@ -314,6 +318,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
314318
SecurityContext context = SecurityContextHolder.createEmptyContext();
315319
context.setAuthentication(authResult);
316320
SecurityContextHolder.setContext(context);
321+
this.securityContextRepository.saveContext(context, request, response);
317322
if (this.logger.isDebugEnabled()) {
318323
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authResult));
319324
}
@@ -435,6 +440,18 @@ public void setAuthenticationFailureHandler(AuthenticationFailureHandler failure
435440
this.failureHandler = failureHandler;
436441
}
437442

443+
/**
444+
* Sets the {@link SecurityContextRepository} to save the {@link SecurityContext} on
445+
* authentication success. The default action is not to save the
446+
* {@link SecurityContext}.
447+
* @param securityContextRepository the {@link SecurityContextRepository} to use.
448+
* Cannot be null.
449+
*/
450+
public void setSecurityContextRepository(SecurityContextRepository securityContextRepository) {
451+
Assert.notNull(securityContextRepository, "securityContextRepository cannot be null");
452+
this.securityContextRepository = securityContextRepository;
453+
}
454+
438455
protected AuthenticationSuccessHandler getSuccessHandler() {
439456
return this.successHandler;
440457
}

web/src/test/java/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilterTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@
2727
import org.junit.jupiter.api.AfterEach;
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
30+
import org.mockito.ArgumentCaptor;
3031

3132
import org.springframework.mock.web.MockFilterConfig;
3233
import org.springframework.mock.web.MockHttpServletRequest;
3334
import org.springframework.mock.web.MockHttpServletResponse;
3435
import org.springframework.security.authentication.AuthenticationManager;
3536
import org.springframework.security.authentication.BadCredentialsException;
3637
import org.springframework.security.authentication.InternalAuthenticationServiceException;
38+
import org.springframework.security.authentication.TestAuthentication;
3739
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3840
import org.springframework.security.core.Authentication;
3941
import org.springframework.security.core.AuthenticationException;
4042
import org.springframework.security.core.authority.AuthorityUtils;
43+
import org.springframework.security.core.context.SecurityContext;
4144
import org.springframework.security.core.context.SecurityContextHolder;
4245
import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServicesTests;
4346
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
4447
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
48+
import org.springframework.security.web.context.SecurityContextRepository;
4549
import org.springframework.security.web.firewall.DefaultHttpFirewall;
4650
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
4751
import org.springframework.security.web.util.matcher.RequestMatcher;
@@ -322,6 +326,37 @@ public void testSuccessfulAuthenticationInvokesSuccessHandlerAndSetsContext() th
322326
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
323327
}
324328

329+
@Test
330+
public void testSuccessfulAuthenticationThenDefaultDoesNotCreateSession() throws Exception {
331+
Authentication authentication = TestAuthentication.authenticatedUser();
332+
MockHttpServletRequest request = new MockHttpServletRequest();
333+
MockHttpServletResponse response = new MockHttpServletResponse();
334+
MockFilterChain chain = new MockFilterChain(false);
335+
MockAuthenticationFilter filter = new MockAuthenticationFilter();
336+
337+
filter.successfulAuthentication(request, response, chain, authentication);
338+
339+
assertThat(request.getSession(false)).isNull();
340+
}
341+
342+
@Test
343+
public void testSuccessfulAuthenticationWhenCustomSecurityContextRepositoryThenAuthenticationSaved()
344+
throws Exception {
345+
ArgumentCaptor<SecurityContext> contextCaptor = ArgumentCaptor.forClass(SecurityContext.class);
346+
SecurityContextRepository repository = mock(SecurityContextRepository.class);
347+
Authentication authentication = TestAuthentication.authenticatedUser();
348+
MockHttpServletRequest request = new MockHttpServletRequest();
349+
MockHttpServletResponse response = new MockHttpServletResponse();
350+
MockFilterChain chain = new MockFilterChain(false);
351+
MockAuthenticationFilter filter = new MockAuthenticationFilter();
352+
filter.setSecurityContextRepository(repository);
353+
354+
filter.successfulAuthentication(request, response, chain, authentication);
355+
356+
verify(repository).saveContext(contextCaptor.capture(), eq(request), eq(response));
357+
assertThat(contextCaptor.getValue().getAuthentication()).isEqualTo(authentication);
358+
}
359+
325360
@Test
326361
public void testFailedAuthenticationInvokesFailureHandler() throws Exception {
327362
// Setup our HTTP request

0 commit comments

Comments
 (0)