|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
32 | 32 | import org.springframework.security.core.AuthenticationException;
|
33 | 33 | import org.springframework.security.core.context.SecurityContext;
|
34 | 34 | import org.springframework.security.core.context.SecurityContextHolder;
|
| 35 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
35 | 36 | import org.springframework.security.web.context.NullSecurityContextRepository;
|
36 | 37 | import org.springframework.security.web.context.SecurityContextRepository;
|
37 | 38 | import org.springframework.security.web.util.matcher.AnyRequestMatcher;
|
|
67 | 68 | */
|
68 | 69 | public class AuthenticationFilter extends OncePerRequestFilter {
|
69 | 70 |
|
| 71 | + private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder |
| 72 | + .getContextHolderStrategy(); |
| 73 | + |
70 | 74 | private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
|
71 | 75 |
|
72 | 76 | private AuthenticationConverter authenticationConverter;
|
@@ -151,6 +155,17 @@ public void setSecurityContextRepository(SecurityContextRepository securityConte
|
151 | 155 | this.securityContextRepository = securityContextRepository;
|
152 | 156 | }
|
153 | 157 |
|
| 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 | + |
154 | 169 | @Override
|
155 | 170 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
156 | 171 | throws ServletException, IOException {
|
@@ -180,15 +195,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
|
180 | 195 |
|
181 | 196 | private void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
|
182 | 197 | AuthenticationException failed) throws IOException, ServletException {
|
183 |
| - SecurityContextHolder.clearContext(); |
| 198 | + this.securityContextHolderStrategy.clearContext(); |
184 | 199 | this.failureHandler.onAuthenticationFailure(request, response, failed);
|
185 | 200 | }
|
186 | 201 |
|
187 | 202 | private void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
|
188 | 203 | Authentication authentication) throws IOException, ServletException {
|
189 |
| - SecurityContext context = SecurityContextHolder.createEmptyContext(); |
| 204 | + SecurityContext context = this.securityContextHolderStrategy.createEmptyContext(); |
190 | 205 | context.setAuthentication(authentication);
|
191 |
| - SecurityContextHolder.setContext(context); |
| 206 | + this.securityContextHolderStrategy.setContext(context); |
192 | 207 | this.securityContextRepository.saveContext(context, request, response);
|
193 | 208 | this.successHandler.onAuthenticationSuccess(request, response, chain, authentication);
|
194 | 209 | }
|
|
0 commit comments