1
0
mirror of synced 2026-08-05 17:57:15 +00:00

AuthenticationEntryPoint->ServerAuthenticationEntryPoint

Issue gh-4615
This commit is contained in:
Rob Winch
2017-10-10 13:32:51 -05:00
parent 8d4a73cf3f
commit cfc5572b7a
15 changed files with 83 additions and 81 deletions
@@ -31,20 +31,21 @@ import java.util.List;
* @author Rob Winch
* @since 5.0
*/
public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint {
public class DelegatingServerAuthenticationEntryPoint
implements ServerAuthenticationEntryPoint {
private final Flux<DelegateEntry> entryPoints;
private AuthenticationEntryPoint defaultEntryPoint = (exchange, e) -> {
private ServerAuthenticationEntryPoint defaultEntryPoint = (exchange, e) -> {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
};
public DelegatingAuthenticationEntryPoint(
public DelegatingServerAuthenticationEntryPoint(
DelegateEntry... entryPoints) {
this(Arrays.asList(entryPoints));
}
public DelegatingAuthenticationEntryPoint(
public DelegatingServerAuthenticationEntryPoint(
List<DelegateEntry> entryPoints) {
this.entryPoints = Flux.fromIterable(entryPoints);
}
@@ -68,16 +69,16 @@ public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPo
* EntryPoint which is used when no RequestMatcher returned true
*/
public void setDefaultEntryPoint(
AuthenticationEntryPoint defaultEntryPoint) {
ServerAuthenticationEntryPoint defaultEntryPoint) {
this.defaultEntryPoint = defaultEntryPoint;
}
public static class DelegateEntry {
private final ServerWebExchangeMatcher matcher;
private final AuthenticationEntryPoint entryPoint;
private final ServerAuthenticationEntryPoint entryPoint;
public DelegateEntry(ServerWebExchangeMatcher matcher,
AuthenticationEntryPoint entryPoint) {
ServerAuthenticationEntryPoint entryPoint) {
this.matcher = matcher;
this.entryPoint = entryPoint;
}
@@ -86,7 +87,7 @@ public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPo
return this.matcher;
}
public AuthenticationEntryPoint getEntryPoint() {
public ServerAuthenticationEntryPoint getEntryPoint() {
return this.entryPoint;
}
}
@@ -25,7 +25,7 @@ import org.springframework.web.server.ServerWebExchange;
* @author Rob Winch
* @since 5.0
*/
public interface AuthenticationEntryPoint {
public interface ServerAuthenticationEntryPoint {
Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e);
}
@@ -17,7 +17,7 @@
package org.springframework.security.web.server.authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
@@ -27,17 +27,18 @@ import reactor.core.publisher.Mono;
* @since 5.0
*/
public class AuthenticationEntryPointFailureHandler implements AuthenticationFailureHandler {
private final AuthenticationEntryPoint authenticationEntryPoint;
private final ServerAuthenticationEntryPoint serverAuthenticationEntryPoint;
public AuthenticationEntryPointFailureHandler(
AuthenticationEntryPoint authenticationEntryPoint) {
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
this.authenticationEntryPoint = authenticationEntryPoint;
ServerAuthenticationEntryPoint serverAuthenticationEntryPoint) {
Assert.notNull(serverAuthenticationEntryPoint, "authenticationEntryPoint cannot be null");
this.serverAuthenticationEntryPoint = serverAuthenticationEntryPoint;
}
@Override
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange,
AuthenticationException exception) {
return this.authenticationEntryPoint.commence(webFilterExchange.getExchange(), exception);
return this.serverAuthenticationEntryPoint
.commence(webFilterExchange.getExchange(), exception);
}
}
@@ -25,7 +25,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.server.ServerHttpBasicAuthenticationConverter;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.www.HttpBasicAuthenticationEntryPoint;
import org.springframework.security.web.server.authentication.www.HttpBasicServerAuthenticationEntryPoint;
import org.springframework.security.web.server.context.SecurityContextRepository;
import org.springframework.security.web.server.context.SecurityContextRepositoryServerWebExchange;
import org.springframework.security.web.server.context.ServerWebExchangeAttributeSecurityContextRepository;
@@ -49,7 +49,7 @@ public class AuthenticationWebFilter implements WebFilter {
private Function<ServerWebExchange,Mono<Authentication>> authenticationConverter = new ServerHttpBasicAuthenticationConverter();
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationEntryPointFailureHandler(new HttpBasicAuthenticationEntryPoint());
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationEntryPointFailureHandler(new HttpBasicServerAuthenticationEntryPoint());
private SecurityContextRepository securityContextRepository = new ServerWebExchangeAttributeSecurityContextRepository();
@@ -23,7 +23,7 @@ import org.springframework.security.web.server.ServerRedirectStrategy;
import reactor.core.publisher.Mono;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
@@ -33,12 +33,13 @@ import org.springframework.web.server.ServerWebExchange;
* @author Rob Winch
* @since 5.0
*/
public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoint {
public class RedirectServerAuthenticationEntryPoint
implements ServerAuthenticationEntryPoint {
private final URI location;
private ServerRedirectStrategy serverRedirectStrategy = new DefaultServerRedirectStrategy();
public RedirectAuthenticationEntryPoint(String location) {
public RedirectServerAuthenticationEntryPoint(String location) {
Assert.notNull(location, "location cannot be null");
this.location = URI.create(location);
}
@@ -18,7 +18,7 @@ package org.springframework.security.web.server.authentication.www;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
@@ -29,7 +29,8 @@ import reactor.core.publisher.Mono;
* @author Rob Winch
* @since 5.0
*/
public class HttpBasicAuthenticationEntryPoint implements AuthenticationEntryPoint {
public class HttpBasicServerAuthenticationEntryPoint
implements ServerAuthenticationEntryPoint {
private static final String WWW_AUTHENTICATE = "WWW-Authenticate";
private static final String DEFAULT_REALM = "Realm";
private static String WWW_AUTHENTICATE_FORMAT = "Basic realm=\"%s\"";
@@ -17,9 +17,6 @@ package org.springframework.security.web.server.authorization;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.authentication.www.HttpBasicAuthenticationEntryPoint;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
@@ -20,8 +20,8 @@ import reactor.core.publisher.Mono;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.authentication.www.HttpBasicAuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.security.web.server.authentication.www.HttpBasicServerAuthenticationEntryPoint;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
@@ -33,7 +33,7 @@ import org.springframework.web.server.WebFilterChain;
* @since 5.0
*/
public class ExceptionTranslationWebFilter implements WebFilter {
private AuthenticationEntryPoint authenticationEntryPoint = new HttpBasicAuthenticationEntryPoint();
private ServerAuthenticationEntryPoint serverAuthenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint();
private AccessDeniedHandler accessDeniedHandler = new HttpStatusAccessDeniedHandler(HttpStatus.FORBIDDEN);
@@ -58,17 +58,17 @@ public class ExceptionTranslationWebFilter implements WebFilter {
/**
* Sets the authentication entry point used when authentication is required
* @param authenticationEntryPoint the authentication entry point to use. Default is
* {@link HttpBasicAuthenticationEntryPoint}
* @param serverAuthenticationEntryPoint the authentication entry point to use. Default is
* {@link HttpBasicServerAuthenticationEntryPoint}
*/
public void setAuthenticationEntryPoint(
AuthenticationEntryPoint authenticationEntryPoint) {
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
this.authenticationEntryPoint = authenticationEntryPoint;
public void setServerAuthenticationEntryPoint(
ServerAuthenticationEntryPoint serverAuthenticationEntryPoint) {
Assert.notNull(serverAuthenticationEntryPoint, "authenticationEntryPoint cannot be null");
this.serverAuthenticationEntryPoint = serverAuthenticationEntryPoint;
}
private <T> Mono<T> commenceAuthentication(ServerWebExchange exchange, AccessDeniedException denied) {
return this.authenticationEntryPoint.commence(exchange, new AuthenticationCredentialsNotFoundException("Not Authenticated", denied))
return this.serverAuthenticationEntryPoint.commence(exchange, new AuthenticationCredentialsNotFoundException("Not Authenticated", denied))
.then(Mono.empty());
}
}
@@ -32,14 +32,14 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.security.web.server.DelegatingAuthenticationEntryPoint.*;
import static org.springframework.security.web.server.DelegatingServerAuthenticationEntryPoint.*;
/**
* @author Rob Winch
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class DelegatingAuthenticationEntryPointTests {
public class DelegatingServerAuthenticationEntryPointTests {
private ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
@Mock
@@ -47,12 +47,12 @@ public class DelegatingAuthenticationEntryPointTests {
@Mock
private ServerWebExchangeMatcher matcher2;
@Mock
private AuthenticationEntryPoint delegate1;
private ServerAuthenticationEntryPoint delegate1;
@Mock
private AuthenticationEntryPoint delegate2;
private ServerAuthenticationEntryPoint delegate2;
private AuthenticationException e = new AuthenticationCredentialsNotFoundException("Log In");
private DelegatingAuthenticationEntryPoint entryPoint;
private DelegatingServerAuthenticationEntryPoint entryPoint;
@Test
public void commenceWhenNotMatchThenMatchThenOnlySecondDelegateInvoked() {
@@ -62,7 +62,7 @@ public class DelegatingAuthenticationEntryPointTests {
when(this.matcher2.matches(this.exchange)).thenReturn(
ServerWebExchangeMatcher.MatchResult.match());
when(this.delegate2.commence(this.exchange, this.e)).thenReturn(expectedResult);
this.entryPoint = new DelegatingAuthenticationEntryPoint(
this.entryPoint = new DelegatingServerAuthenticationEntryPoint(
new DelegateEntry(this.matcher1, this.delegate1),
new DelegateEntry(this.matcher2, this.delegate2));
@@ -78,7 +78,7 @@ public class DelegatingAuthenticationEntryPointTests {
public void commenceWhenNotMatchThenDefault() {
when(this.matcher1.matches(this.exchange)).thenReturn(
ServerWebExchangeMatcher.MatchResult.notMatch());
this.entryPoint = new DelegatingAuthenticationEntryPoint(
this.entryPoint = new DelegatingServerAuthenticationEntryPoint(
new DelegateEntry(this.matcher1, this.delegate1));
this.entryPoint.commence(this.exchange, this.e).block();
@@ -61,7 +61,7 @@ public class RedirectAuthenticationSuccessHandlerTests {
@Test(expected = IllegalArgumentException.class)
public void constructorStringWhenNullLocationThenException() {
new RedirectAuthenticationEntryPoint((String) null);
new RedirectServerAuthenticationEntryPoint((String) null);
}
@Test
@@ -39,7 +39,7 @@ import static org.mockito.Mockito.when;
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class RedirectAuthenticationEntryPointTests {
public class RedirectServerAuthenticationEntryPointTests {
@Mock
private ServerWebExchange exchange;
@@ -48,15 +48,15 @@ public class RedirectAuthenticationEntryPointTests {
private String location = "/login";
private RedirectAuthenticationEntryPoint entryPoint =
new RedirectAuthenticationEntryPoint("/login");
private RedirectServerAuthenticationEntryPoint entryPoint =
new RedirectServerAuthenticationEntryPoint("/login");
private AuthenticationException exception = new AuthenticationCredentialsNotFoundException("Authentication Required");
@Test(expected = IllegalArgumentException.class)
public void constructorStringWhenNullLocationThenException() {
new RedirectAuthenticationEntryPoint((String) null);
new RedirectServerAuthenticationEntryPoint((String) null);
}
@Test
@@ -24,7 +24,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import reactor.core.publisher.Mono;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
@@ -37,9 +37,9 @@ import static org.mockito.Mockito.when;
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthenticationEntryPointFailureHandlerTests {
public class ServerAuthenticationEntryPointFailureHandlerTests {
@Mock
private AuthenticationEntryPoint authenticationEntryPoint;
private ServerAuthenticationEntryPoint serverAuthenticationEntryPoint;
@Mock
private ServerWebExchange exchange;
@Mock
@@ -53,15 +53,15 @@ public class AuthenticationEntryPointFailureHandlerTests {
@Test(expected = IllegalArgumentException.class)
public void constructorWhenNullEntryPointThenException() {
this.authenticationEntryPoint = null;
new AuthenticationEntryPointFailureHandler(this.authenticationEntryPoint);
this.serverAuthenticationEntryPoint = null;
new AuthenticationEntryPointFailureHandler(this.serverAuthenticationEntryPoint);
}
@Test
public void onAuthenticationFailureWhenInvokedThenDelegatesToEntryPoint() {
Mono<Void> result = Mono.empty();
BadCredentialsException e = new BadCredentialsException("Failed");
when(this.authenticationEntryPoint.commence(this.exchange, e)).thenReturn(result);
when(this.serverAuthenticationEntryPoint.commence(this.exchange, e)).thenReturn(result);
assertThat(this.handler.onAuthenticationFailure(this.filterExchange, e)).isEqualTo(result);
}
@@ -35,10 +35,10 @@ import static org.mockito.Mockito.verifyZeroInteractions;
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class HttpBasicAuthenticationEntryPointTests {
public class HttpBasicServerAuthenticationEntryPointTests {
@Mock
private ServerWebExchange exchange;
private HttpBasicAuthenticationEntryPoint entryPoint = new HttpBasicAuthenticationEntryPoint();
private HttpBasicServerAuthenticationEntryPoint entryPoint = new HttpBasicServerAuthenticationEntryPoint();
private AuthenticationException exception = new AuthenticationCredentialsNotFoundException("Authenticate");
@@ -30,7 +30,7 @@ import reactor.test.publisher.PublisherProbe;
import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.server.AuthenticationEntryPoint;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
@@ -53,7 +53,7 @@ public class ExceptionTranslationWebFilterTests {
@Mock
private AccessDeniedHandler deniedHandler;
@Mock
private AuthenticationEntryPoint entryPoint;
private ServerAuthenticationEntryPoint entryPoint;
private PublisherProbe<Void> deniedPublisher = PublisherProbe.empty();
private PublisherProbe<Void> entryPointPublisher = PublisherProbe.empty();
@@ -66,7 +66,7 @@ public class ExceptionTranslationWebFilterTests {
when(this.deniedHandler.handle(any(), any())).thenReturn(this.deniedPublisher.mono());
when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointPublisher.mono());
this.filter.setAuthenticationEntryPoint(this.entryPoint);
this.filter.setServerAuthenticationEntryPoint(this.entryPoint);
this.filter.setAccessDeniedHandler(this.deniedHandler);
}
@@ -155,6 +155,6 @@ public class ExceptionTranslationWebFilterTests {
@Test(expected = IllegalArgumentException.class)
public void setAuthenticationEntryPointWhenNullThenException() {
this.filter.setAuthenticationEntryPoint(null);
this.filter.setServerAuthenticationEntryPoint(null);
}
}