|
@@ -36,6 +36,27 @@ import org.springframework.web.server.WebFilter;
|
|
import org.springframework.web.server.WebFilterChain;
|
|
import org.springframework.web.server.WebFilterChain;
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * A {@link WebFilter} that performs authentication of a particular request. An outline of the logic:
|
|
|
|
+ *
|
|
|
|
+ * <ul>
|
|
|
|
+ * <li>
|
|
|
|
+ * A request comes in and if it does not match {@link #setRequiresAuthenticationMatcher(ServerWebExchangeMatcher)},
|
|
|
|
+ * then this filter does nothing and the {@link WebFilterChain} is continued. If it does match then...
|
|
|
|
+ * </li>
|
|
|
|
+ * <li>
|
|
|
|
+ * An attempt to convert the {@link ServerWebExchange} into an {@link Authentication} is made. If the result is
|
|
|
|
+ * empty, then the filter does nothing more and the {@link WebFilterChain} is continued. If it does create an
|
|
|
|
+ * {@link Authentication}...
|
|
|
|
+ * </li>
|
|
|
|
+ * <li>
|
|
|
|
+ * The {@link ReactiveAuthenticationManager} specified in
|
|
|
|
+ * {@link #AuthenticationWebFilter(ReactiveAuthenticationManager)} is used to perform authentication.
|
|
|
|
+ * </li>
|
|
|
|
+ * <li>
|
|
|
|
+ * If authentication is successful, {@link ServerAuthenticationSuccessHandler} is invoked and the authentication
|
|
|
|
+ * is set on {@link ReactiveSecurityContextHolder}, else {@link ServerAuthenticationFailureHandler} is invoked
|
|
|
|
+ * </li>
|
|
|
|
+ * </ul>
|
|
*
|
|
*
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
* @since 5.0
|
|
* @since 5.0
|
|
@@ -54,6 +75,10 @@ public class AuthenticationWebFilter implements WebFilter {
|
|
|
|
|
|
private ServerWebExchangeMatcher requiresAuthenticationMatcher = ServerWebExchangeMatchers.anyExchange();
|
|
private ServerWebExchangeMatcher requiresAuthenticationMatcher = ServerWebExchangeMatchers.anyExchange();
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Creates an instance
|
|
|
|
+ * @param authenticationManager the authentication manager to use
|
|
|
|
+ */
|
|
public AuthenticationWebFilter(ReactiveAuthenticationManager authenticationManager) {
|
|
public AuthenticationWebFilter(ReactiveAuthenticationManager authenticationManager) {
|
|
Assert.notNull(authenticationManager, "authenticationManager cannot be null");
|
|
Assert.notNull(authenticationManager, "authenticationManager cannot be null");
|
|
this.authenticationManager = authenticationManager;
|
|
this.authenticationManager = authenticationManager;
|
|
@@ -87,26 +112,53 @@ public class AuthenticationWebFilter implements WebFilter {
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)));
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the repository for persisting the SecurityContext. Default is {@link NoOpServerSecurityContextRepository}
|
|
|
|
+ * @param securityContextRepository the repository to use
|
|
|
|
+ */
|
|
public void setSecurityContextRepository(
|
|
public void setSecurityContextRepository(
|
|
ServerSecurityContextRepository securityContextRepository) {
|
|
ServerSecurityContextRepository securityContextRepository) {
|
|
Assert.notNull(securityContextRepository, "securityContextRepository cannot be null");
|
|
Assert.notNull(securityContextRepository, "securityContextRepository cannot be null");
|
|
this.securityContextRepository = securityContextRepository;
|
|
this.securityContextRepository = securityContextRepository;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the authentication success handler. Default is {@link WebFilterChainServerAuthenticationSuccessHandler}
|
|
|
|
+ * @param authenticationSuccessHandler the success handler to use
|
|
|
|
+ */
|
|
public void setAuthenticationSuccessHandler(ServerAuthenticationSuccessHandler authenticationSuccessHandler) {
|
|
public void setAuthenticationSuccessHandler(ServerAuthenticationSuccessHandler authenticationSuccessHandler) {
|
|
|
|
+ Assert.notNull(authenticationSuccessHandler, "authenticationSuccessHandler cannot be null");
|
|
this.authenticationSuccessHandler = authenticationSuccessHandler;
|
|
this.authenticationSuccessHandler = authenticationSuccessHandler;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the strategy used for converting from a {@link ServerWebExchange} to an {@link Authentication} used for
|
|
|
|
+ * authenticating with the provided {@link ReactiveAuthenticationManager}. If the result is empty, then it signals
|
|
|
|
+ * that no authentication attempt should be made. The default converter is
|
|
|
|
+ * {@link ServerHttpBasicAuthenticationConverter}
|
|
|
|
+ * @param authenticationConverter the converter to use
|
|
|
|
+ */
|
|
public void setAuthenticationConverter(Function<ServerWebExchange, Mono<Authentication>> authenticationConverter) {
|
|
public void setAuthenticationConverter(Function<ServerWebExchange, Mono<Authentication>> authenticationConverter) {
|
|
|
|
+ Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
|
this.authenticationConverter = authenticationConverter;
|
|
this.authenticationConverter = authenticationConverter;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the failure handler used when authentication fails. The default is to prompt for basic authentication.
|
|
|
|
+ * @param authenticationFailureHandler the handler to use. Cannot be null.
|
|
|
|
+ */
|
|
public void setAuthenticationFailureHandler(
|
|
public void setAuthenticationFailureHandler(
|
|
ServerAuthenticationFailureHandler authenticationFailureHandler) {
|
|
ServerAuthenticationFailureHandler authenticationFailureHandler) {
|
|
Assert.notNull(authenticationFailureHandler, "authenticationFailureHandler cannot be null");
|
|
Assert.notNull(authenticationFailureHandler, "authenticationFailureHandler cannot be null");
|
|
this.authenticationFailureHandler = authenticationFailureHandler;
|
|
this.authenticationFailureHandler = authenticationFailureHandler;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the matcher used to determine when creating an {@link Authentication} from
|
|
|
|
+ * {@link #setAuthenticationConverter(Function)} to be authentication. If the converter returns an empty
|
|
|
|
+ * result, then no authentication is attempted. The default is any request
|
|
|
|
+ * @param requiresAuthenticationMatcher the matcher to use. Cannot be null.
|
|
|
|
+ */
|
|
public void setRequiresAuthenticationMatcher(
|
|
public void setRequiresAuthenticationMatcher(
|
|
ServerWebExchangeMatcher requiresAuthenticationMatcher) {
|
|
ServerWebExchangeMatcher requiresAuthenticationMatcher) {
|
|
Assert.notNull(requiresAuthenticationMatcher, "requiresAuthenticationMatcher cannot be null");
|
|
Assert.notNull(requiresAuthenticationMatcher, "requiresAuthenticationMatcher cannot be null");
|