AuthenticationFailureHandler->ServerAuthenticationFailureHandler

Issue gh-4615
This commit is contained in:
Rob Winch 2017-10-10 15:58:20 -05:00
parent 897e7111e3
commit c9ce528206
6 changed files with 23 additions and 21 deletions

View File

@ -30,8 +30,8 @@ import org.springframework.security.web.server.ServerFormLoginAuthenticationConv
import org.springframework.security.web.server.ServerHttpBasicAuthenticationConverter;
import org.springframework.security.web.server.MatcherSecurityWebFilterChain;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.AuthenticationEntryPointFailureHandler;
import org.springframework.security.web.server.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.server.authentication.ServerAuthenticationEntryPointFailureHandler;
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationEntryPoint;
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
@ -355,7 +355,7 @@ public class HttpSecurity {
HttpSecurity.this.defaultEntryPoints.add(new DelegateEntry(restMatcher, this.entryPoint));
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(
this.authenticationManager);
authenticationFilter.setAuthenticationFailureHandler(new AuthenticationEntryPointFailureHandler(this.entryPoint));
authenticationFilter.setServerAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(this.entryPoint));
authenticationFilter.setAuthenticationConverter(new ServerHttpBasicAuthenticationConverter());
if(this.securityContextServerRepository != null) {
authenticationFilter.setSecurityContextServerRepository(this.securityContextServerRepository);
@ -379,7 +379,7 @@ public class HttpSecurity {
private ServerWebExchangeMatcher requiresAuthenticationMatcher;
private AuthenticationFailureHandler authenticationFailureHandler;
private ServerAuthenticationFailureHandler serverAuthenticationFailureHandler;
public FormLoginBuilder authenticationManager(ReactiveAuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
@ -389,7 +389,7 @@ public class HttpSecurity {
public FormLoginBuilder loginPage(String loginPage) {
this.serverAuthenticationEntryPoint = new RedirectServerAuthenticationEntryPoint(loginPage);
this.requiresAuthenticationMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, loginPage);
this.authenticationFailureHandler = new AuthenticationEntryPointFailureHandler(new RedirectServerAuthenticationEntryPoint(loginPage + "?error"));
this.serverAuthenticationFailureHandler = new ServerAuthenticationEntryPointFailureHandler(new RedirectServerAuthenticationEntryPoint(loginPage + "?error"));
return this;
}
@ -403,8 +403,8 @@ public class HttpSecurity {
return this;
}
public FormLoginBuilder authenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
this.authenticationFailureHandler = authenticationFailureHandler;
public FormLoginBuilder authenticationFailureHandler(ServerAuthenticationFailureHandler serverAuthenticationFailureHandler) {
this.serverAuthenticationFailureHandler = serverAuthenticationFailureHandler;
return this;
}
@ -433,7 +433,7 @@ public class HttpSecurity {
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(
this.authenticationManager);
authenticationFilter.setRequiresAuthenticationMatcher(this.requiresAuthenticationMatcher);
authenticationFilter.setAuthenticationFailureHandler(this.authenticationFailureHandler);
authenticationFilter.setServerAuthenticationFailureHandler(this.serverAuthenticationFailureHandler);
authenticationFilter.setAuthenticationConverter(new ServerFormLoginAuthenticationConverter());
authenticationFilter.setServerAuthenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/"));
authenticationFilter.setSecurityContextServerRepository(this.securityContextServerRepository);

View File

@ -49,7 +49,7 @@ public class AuthenticationWebFilter implements WebFilter {
private Function<ServerWebExchange,Mono<Authentication>> authenticationConverter = new ServerHttpBasicAuthenticationConverter();
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint());
private ServerAuthenticationFailureHandler serverAuthenticationFailureHandler = new ServerAuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint());
private SecurityContextServerRepository securityContextServerRepository = new ServerWebExchangeAttributeSecurityContextServerRepository();
@ -79,7 +79,8 @@ public class AuthenticationWebFilter implements WebFilter {
WebFilterExchange webFilterExchange = new WebFilterExchange(wrappedExchange, chain);
return this.authenticationManager.authenticate(token)
.flatMap(authentication -> onAuthenticationSuccess(authentication, webFilterExchange))
.onErrorResume(AuthenticationException.class, e -> this.authenticationFailureHandler.onAuthenticationFailure(webFilterExchange, e));
.onErrorResume(AuthenticationException.class, e -> this.serverAuthenticationFailureHandler
.onAuthenticationFailure(webFilterExchange, e));
}
private Mono<Void> onAuthenticationSuccess(Authentication authentication, WebFilterExchange webFilterExchange) {
@ -105,10 +106,10 @@ public class AuthenticationWebFilter implements WebFilter {
this.authenticationConverter = authenticationConverter;
}
public void setAuthenticationFailureHandler(
AuthenticationFailureHandler authenticationFailureHandler) {
Assert.notNull(authenticationFailureHandler, "authenticationFailureHandler cannot be null");
this.authenticationFailureHandler = authenticationFailureHandler;
public void setServerAuthenticationFailureHandler(
ServerAuthenticationFailureHandler serverAuthenticationFailureHandler) {
Assert.notNull(serverAuthenticationFailureHandler, "authenticationFailureHandler cannot be null");
this.serverAuthenticationFailureHandler = serverAuthenticationFailureHandler;
}
public void setRequiresAuthenticationMatcher(

View File

@ -26,10 +26,11 @@ import reactor.core.publisher.Mono;
* @author Rob Winch
* @since 5.0
*/
public class AuthenticationEntryPointFailureHandler implements AuthenticationFailureHandler {
public class ServerAuthenticationEntryPointFailureHandler
implements ServerAuthenticationFailureHandler {
private final ServerAuthenticationEntryPoint serverAuthenticationEntryPoint;
public AuthenticationEntryPointFailureHandler(
public ServerAuthenticationEntryPointFailureHandler(
ServerAuthenticationEntryPoint serverAuthenticationEntryPoint) {
Assert.notNull(serverAuthenticationEntryPoint, "authenticationEntryPoint cannot be null");
this.serverAuthenticationEntryPoint = serverAuthenticationEntryPoint;

View File

@ -26,6 +26,6 @@ import org.springframework.security.web.server.WebFilterExchange;
* @author Rob Winch
* @since 5.0
*/
public interface AuthenticationFailureHandler {
public interface ServerAuthenticationFailureHandler {
Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception);
}

View File

@ -60,7 +60,7 @@ public class AuthenticationWebFilterTests {
@Mock
private ReactiveAuthenticationManager authenticationManager;
@Mock
private AuthenticationFailureHandler failureHandler;
private ServerAuthenticationFailureHandler failureHandler;
@Mock
private SecurityContextServerRepository securityContextServerRepository;
@ -72,7 +72,7 @@ public class AuthenticationWebFilterTests {
this.filter.setServerAuthenticationSuccessHandler(this.successHandler);
this.filter.setAuthenticationConverter(this.authenticationConverter);
this.filter.setSecurityContextServerRepository(this.securityContextServerRepository);
this.filter.setAuthenticationFailureHandler(this.failureHandler);
this.filter.setServerAuthenticationFailureHandler(this.failureHandler);
}
@Test

View File

@ -49,12 +49,12 @@ public class ServerAuthenticationEntryPointFailureHandlerTests {
private WebFilterExchange filterExchange;
@InjectMocks
private AuthenticationEntryPointFailureHandler handler;
private ServerAuthenticationEntryPointFailureHandler handler;
@Test(expected = IllegalArgumentException.class)
public void constructorWhenNullEntryPointThenException() {
this.serverAuthenticationEntryPoint = null;
new AuthenticationEntryPointFailureHandler(this.serverAuthenticationEntryPoint);
new ServerAuthenticationEntryPointFailureHandler(this.serverAuthenticationEntryPoint);
}
@Test