|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2021 the original author or authors.
|
|
|
+ * Copyright 2002-2022 the original author or authors.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -32,6 +32,7 @@ import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider;
|
|
@@ -64,6 +65,9 @@ public final class BearerTokenAuthenticationFilter extends OncePerRequestFilter
|
|
|
|
|
|
private final AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver;
|
|
|
|
|
|
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
|
|
+ .getContextHolderStrategy();
|
|
|
+
|
|
|
private AuthenticationEntryPoint authenticationEntryPoint = new BearerTokenAuthenticationEntryPoint();
|
|
|
|
|
|
private AuthenticationFailureHandler authenticationFailureHandler = (request, response, exception) -> {
|
|
@@ -132,9 +136,9 @@ public final class BearerTokenAuthenticationFilter extends OncePerRequestFilter
|
|
|
try {
|
|
|
AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request);
|
|
|
Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest);
|
|
|
- SecurityContext context = SecurityContextHolder.createEmptyContext();
|
|
|
+ SecurityContext context = this.securityContextHolderStrategy.createEmptyContext();
|
|
|
context.setAuthentication(authenticationResult);
|
|
|
- SecurityContextHolder.setContext(context);
|
|
|
+ this.securityContextHolderStrategy.setContext(context);
|
|
|
this.securityContextRepository.saveContext(context, request, response);
|
|
|
if (this.logger.isDebugEnabled()) {
|
|
|
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authenticationResult));
|
|
@@ -142,12 +146,23 @@ public final class BearerTokenAuthenticationFilter extends OncePerRequestFilter
|
|
|
filterChain.doFilter(request, response);
|
|
|
}
|
|
|
catch (AuthenticationException failed) {
|
|
|
- SecurityContextHolder.clearContext();
|
|
|
+ this.securityContextHolderStrategy.clearContext();
|
|
|
this.logger.trace("Failed to process authentication request", failed);
|
|
|
this.authenticationFailureHandler.onAuthenticationFailure(request, response, failed);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
|
|
|
+ * the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
|
|
|
+ *
|
|
|
+ * @since 5.8
|
|
|
+ */
|
|
|
+ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
|
|
|
+ Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
|
|
|
+ this.securityContextHolderStrategy = securityContextHolderStrategy;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Sets the {@link SecurityContextRepository} to save the {@link SecurityContext} on
|
|
|
* authentication success. The default action is not to save the
|