Skip to content

Commit f3d99f5

Browse files
committed
Use SecurityContextHolderStrategy for AuthenticationFilter
Issue gh-11060
1 parent bffe084 commit f3d99f5

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.security.core.AuthenticationException;
3333
import org.springframework.security.core.context.SecurityContext;
3434
import org.springframework.security.core.context.SecurityContextHolder;
35+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
3536
import org.springframework.security.web.context.NullSecurityContextRepository;
3637
import org.springframework.security.web.context.SecurityContextRepository;
3738
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
@@ -67,6 +68,9 @@
6768
*/
6869
public class AuthenticationFilter extends OncePerRequestFilter {
6970

71+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
72+
.getContextHolderStrategy();
73+
7074
private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
7175

7276
private AuthenticationConverter authenticationConverter;
@@ -151,6 +155,17 @@ public void setSecurityContextRepository(SecurityContextRepository securityConte
151155
this.securityContextRepository = securityContextRepository;
152156
}
153157

158+
/**
159+
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
160+
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
161+
*
162+
* @since 5.8
163+
*/
164+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
165+
Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
166+
this.securityContextHolderStrategy = securityContextHolderStrategy;
167+
}
168+
154169
@Override
155170
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
156171
throws ServletException, IOException {
@@ -180,15 +195,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
180195

181196
private void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
182197
AuthenticationException failed) throws IOException, ServletException {
183-
SecurityContextHolder.clearContext();
198+
this.securityContextHolderStrategy.clearContext();
184199
this.failureHandler.onAuthenticationFailure(request, response, failed);
185200
}
186201

187202
private void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
188203
Authentication authentication) throws IOException, ServletException {
189-
SecurityContext context = SecurityContextHolder.createEmptyContext();
204+
SecurityContext context = this.securityContextHolderStrategy.createEmptyContext();
190205
context.setAuthentication(authentication);
191-
SecurityContextHolder.setContext(context);
206+
this.securityContextHolderStrategy.setContext(context);
192207
this.securityContextRepository.saveContext(context, request, response);
193208
this.successHandler.onAuthenticationSuccess(request, response, chain, authentication);
194209
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@
4040
import org.springframework.security.core.Authentication;
4141
import org.springframework.security.core.context.SecurityContext;
4242
import org.springframework.security.core.context.SecurityContextHolder;
43+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
44+
import org.springframework.security.core.context.SecurityContextImpl;
4345
import org.springframework.security.web.context.SecurityContextRepository;
4446
import org.springframework.security.web.util.matcher.RequestMatcher;
4547

@@ -128,6 +130,25 @@ public void filterWhenDefaultsAndAuthenticationSuccessThenContinues() throws Exc
128130
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
129131
}
130132

133+
@Test
134+
public void filterWhenCustomSecurityContextHolderStrategyThenUses() throws Exception {
135+
Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE");
136+
given(this.authenticationConverter.convert(any())).willReturn(authentication);
137+
given(this.authenticationManager.authenticate(any())).willReturn(authentication);
138+
AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManager,
139+
this.authenticationConverter);
140+
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
141+
given(strategy.createEmptyContext()).willReturn(new SecurityContextImpl());
142+
filter.setSecurityContextHolderStrategy(strategy);
143+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
144+
MockHttpServletResponse response = new MockHttpServletResponse();
145+
FilterChain chain = mock(FilterChain.class);
146+
filter.doFilter(request, response, chain);
147+
verify(this.authenticationManager).authenticate(any(Authentication.class));
148+
verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
149+
verify(strategy).setContext(any());
150+
}
151+
131152
@Test
132153
public void filterWhenAuthenticationManagerResolverDefaultsAndAuthenticationSuccessThenContinues()
133154
throws Exception {

0 commit comments

Comments
 (0)