Add ServerAuthenticationConverter interface
- Adding an ServerAuthenticationConverter interface - Retro-fitting ServerOAuth2LoginAuthenticationTokenConverter, ServerBearerTokenAuthentivationConverter, ServerFormLoginAuthenticationConverter, and ServerHttpBasicAuthenticationConverter to implement ServerAuthenticationConverter - Deprecate existing AuthenticationWebFilter.setAuthenticationConverter and add overloaded one which takes ServerAuthenticationConverter Fixes gh-5338
This commit is contained in:
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.security.web.server;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
|
||||
import org.springframework.util.Assert;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -32,14 +31,14 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ServerFormLoginAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
|
||||
public class ServerFormLoginAuthenticationConverter implements ServerAuthenticationConverter {
|
||||
|
||||
private String usernameParameter = "username";
|
||||
|
||||
private String passwordParameter = "password";
|
||||
|
||||
@Override
|
||||
public Mono<Authentication> apply(ServerWebExchange exchange) {
|
||||
public Mono<Authentication> convert(ServerWebExchange exchange) {
|
||||
return exchange.getFormData()
|
||||
.map( data -> createAuthentication(data));
|
||||
}
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
package org.springframework.security.web.server;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -32,12 +32,12 @@ import reactor.core.publisher.Mono;
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ServerHttpBasicAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
|
||||
public class ServerHttpBasicAuthenticationConverter implements ServerAuthenticationConverter {
|
||||
|
||||
public static final String BASIC = "Basic ";
|
||||
|
||||
@Override
|
||||
public Mono<Authentication> apply(ServerWebExchange exchange) {
|
||||
public Mono<Authentication> convert(ServerWebExchange exchange) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
|
||||
String authorization = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
|
||||
|
||||
@@ -67,7 +67,7 @@ public class AuthenticationWebFilter implements WebFilter {
|
||||
|
||||
private ServerAuthenticationSuccessHandler authenticationSuccessHandler = new WebFilterChainServerAuthenticationSuccessHandler();
|
||||
|
||||
private Function<ServerWebExchange, Mono<Authentication>> authenticationConverter = new ServerHttpBasicAuthenticationConverter();
|
||||
private ServerAuthenticationConverter authenticationConverter = new ServerHttpBasicAuthenticationConverter();
|
||||
|
||||
private ServerAuthenticationFailureHandler authenticationFailureHandler = new ServerAuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint());
|
||||
|
||||
@@ -88,7 +88,7 @@ public class AuthenticationWebFilter implements WebFilter {
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
return this.requiresAuthenticationMatcher.matches(exchange)
|
||||
.filter( matchResult -> matchResult.isMatch())
|
||||
.flatMap( matchResult -> this.authenticationConverter.apply(exchange))
|
||||
.flatMap( matchResult -> this.authenticationConverter.convert(exchange))
|
||||
.switchIfEmpty(chain.filter(exchange).then(Mono.empty()))
|
||||
.flatMap( token -> authenticate(exchange, chain, token));
|
||||
}
|
||||
@@ -138,8 +138,24 @@ public class AuthenticationWebFilter implements WebFilter {
|
||||
* that no authentication attempt should be made. The default converter is
|
||||
* {@link ServerHttpBasicAuthenticationConverter}
|
||||
* @param authenticationConverter the converter to use
|
||||
* @deprecated As of 5.1 in favor of {@link #setAuthenticationConverter(ServerAuthenticationConverter)}
|
||||
* @see #setAuthenticationConverter(ServerAuthenticationConverter)
|
||||
*/
|
||||
@Deprecated
|
||||
public void setAuthenticationConverter(Function<ServerWebExchange, Mono<Authentication>> authenticationConverter) {
|
||||
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
||||
setAuthenticationConverter((ServerAuthenticationConverter) authenticationConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @since 5.1
|
||||
*/
|
||||
public void setAuthenticationConverter(ServerAuthenticationConverter authenticationConverter) {
|
||||
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
|
||||
this.authenticationConverter = authenticationConverter;
|
||||
}
|
||||
@@ -156,7 +172,7 @@ public class AuthenticationWebFilter implements WebFilter {
|
||||
|
||||
/**
|
||||
* Sets the matcher used to determine when creating an {@link Authentication} from
|
||||
* {@link #setAuthenticationConverter(Function)} to be authentication. If the converter returns an empty
|
||||
* {@link #setAuthenticationConverter(ServerAuthenticationConverter)} 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.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.server.authentication;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* A strategy used for converting from a {@link ServerWebExchange} to an {@link Authentication} used for
|
||||
* authenticating with a provided {@link org.springframework.security.authentication.ReactiveAuthenticationManager}.
|
||||
* If the result is {@link Mono#empty()}, then it signals that no authentication attempt should be made.
|
||||
*
|
||||
* @author Eric Deandrea
|
||||
* @since 5.1
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ServerAuthenticationConverter {
|
||||
/**
|
||||
* Converts a {@link ServerWebExchange} to an {@link Authentication}
|
||||
* @param exchange The {@link ServerWebExchange}
|
||||
* @return A {@link Mono} representing an {@link Authentication}
|
||||
*/
|
||||
Mono<Authentication> convert(ServerWebExchange exchange);
|
||||
}
|
||||
Reference in New Issue
Block a user