|
|
|
@ -32,6 +32,7 @@ import org.springframework.security.core.Authentication; |
|
|
|
import org.springframework.security.core.AuthenticationException; |
|
|
|
import org.springframework.security.core.AuthenticationException; |
|
|
|
import org.springframework.security.core.context.SecurityContext; |
|
|
|
import org.springframework.security.core.context.SecurityContext; |
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
|
|
|
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.core.OAuth2AuthenticationException; |
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken; |
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken; |
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider; |
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider; |
|
|
|
@ -67,6 +68,9 @@ public class BearerTokenAuthenticationFilter extends OncePerRequestFilter { |
|
|
|
|
|
|
|
|
|
|
|
private final AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver; |
|
|
|
private final AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder |
|
|
|
|
|
|
|
.getContextHolderStrategy(); |
|
|
|
|
|
|
|
|
|
|
|
private AuthenticationEntryPoint authenticationEntryPoint = new BearerTokenAuthenticationEntryPoint(); |
|
|
|
private AuthenticationEntryPoint authenticationEntryPoint = new BearerTokenAuthenticationEntryPoint(); |
|
|
|
|
|
|
|
|
|
|
|
private AuthenticationFailureHandler authenticationFailureHandler = (request, response, exception) -> { |
|
|
|
private AuthenticationFailureHandler authenticationFailureHandler = (request, response, exception) -> { |
|
|
|
@ -135,9 +139,9 @@ public class BearerTokenAuthenticationFilter extends OncePerRequestFilter { |
|
|
|
try { |
|
|
|
try { |
|
|
|
AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request); |
|
|
|
AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request); |
|
|
|
Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest); |
|
|
|
Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest); |
|
|
|
SecurityContext context = SecurityContextHolder.createEmptyContext(); |
|
|
|
SecurityContext context = this.securityContextHolderStrategy.createEmptyContext(); |
|
|
|
context.setAuthentication(authenticationResult); |
|
|
|
context.setAuthentication(authenticationResult); |
|
|
|
SecurityContextHolder.setContext(context); |
|
|
|
this.securityContextHolderStrategy.setContext(context); |
|
|
|
this.securityContextRepository.saveContext(context, request, response); |
|
|
|
this.securityContextRepository.saveContext(context, request, response); |
|
|
|
if (this.logger.isDebugEnabled()) { |
|
|
|
if (this.logger.isDebugEnabled()) { |
|
|
|
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authenticationResult)); |
|
|
|
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authenticationResult)); |
|
|
|
@ -145,12 +149,23 @@ public class BearerTokenAuthenticationFilter extends OncePerRequestFilter { |
|
|
|
filterChain.doFilter(request, response); |
|
|
|
filterChain.doFilter(request, response); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (AuthenticationException failed) { |
|
|
|
catch (AuthenticationException failed) { |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
this.securityContextHolderStrategy.clearContext(); |
|
|
|
this.logger.trace("Failed to process authentication request", failed); |
|
|
|
this.logger.trace("Failed to process authentication request", failed); |
|
|
|
this.authenticationFailureHandler.onAuthenticationFailure(request, response, 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 |
|
|
|
* Sets the {@link SecurityContextRepository} to save the {@link SecurityContext} on |
|
|
|
* authentication success. The default action is not to save the |
|
|
|
* authentication success. The default action is not to save the |
|
|
|
|