serverAccessDeniedHandler->accessDeniedHandler

Issue: gh-4822
This commit is contained in:
Rob Winch 2017-11-14 15:18:26 -06:00
parent 9e82fc0b83
commit 3bfda6cff7
4 changed files with 18 additions and 17 deletions

View File

@ -393,9 +393,9 @@ public class ServerHttpSecurity {
public class CsrfSpec { public class CsrfSpec {
private CsrfWebFilter filter = new CsrfWebFilter(); private CsrfWebFilter filter = new CsrfWebFilter();
public CsrfSpec serverAccessDeniedHandler( public CsrfSpec accessDeniedHandler(
ServerAccessDeniedHandler serverAccessDeniedHandler) { ServerAccessDeniedHandler accessDeniedHandler) {
this.filter.setServerAccessDeniedHandler(serverAccessDeniedHandler); this.filter.setAccessDeniedHandler(accessDeniedHandler);
return this; return this;
} }

View File

@ -35,26 +35,26 @@ import org.springframework.web.server.WebFilterChain;
public class ExceptionTranslationWebFilter implements WebFilter { public class ExceptionTranslationWebFilter implements WebFilter {
private ServerAuthenticationEntryPoint authenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint(); private ServerAuthenticationEntryPoint authenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint();
private ServerAccessDeniedHandler serverAccessDeniedHandler = new HttpStatusServerAccessDeniedHandler(HttpStatus.FORBIDDEN); private ServerAccessDeniedHandler accessDeniedHandler = new HttpStatusServerAccessDeniedHandler(HttpStatus.FORBIDDEN);
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange) return chain.filter(exchange)
.onErrorResume(AccessDeniedException.class, denied -> exchange.getPrincipal() .onErrorResume(AccessDeniedException.class, denied -> exchange.getPrincipal()
.switchIfEmpty( commenceAuthentication(exchange, denied)) .switchIfEmpty( commenceAuthentication(exchange, denied))
.flatMap( principal -> this.serverAccessDeniedHandler .flatMap( principal -> this.accessDeniedHandler
.handle(exchange, denied)) .handle(exchange, denied))
); );
} }
/** /**
* Sets the access denied handler. * Sets the access denied handler.
* @param serverAccessDeniedHandler the access denied handler to use. Default is * @param accessDeniedHandler the access denied handler to use. Default is
* HttpStatusAccessDeniedHandler with HttpStatus.FORBIDDEN * HttpStatusAccessDeniedHandler with HttpStatus.FORBIDDEN
*/ */
public void setServerAccessDeniedHandler(ServerAccessDeniedHandler serverAccessDeniedHandler) { public void setAccessDeniedHandler(ServerAccessDeniedHandler accessDeniedHandler) {
Assert.notNull(serverAccessDeniedHandler, "accessDeniedHandler cannot be null"); Assert.notNull(accessDeniedHandler, "accessDeniedHandler cannot be null");
this.serverAccessDeniedHandler = serverAccessDeniedHandler; this.accessDeniedHandler = accessDeniedHandler;
} }
/** /**

View File

@ -57,12 +57,12 @@ public class CsrfWebFilter implements WebFilter {
private ServerCsrfTokenRepository serverCsrfTokenRepository = new WebSessionServerCsrfTokenRepository(); private ServerCsrfTokenRepository serverCsrfTokenRepository = new WebSessionServerCsrfTokenRepository();
private ServerAccessDeniedHandler serverAccessDeniedHandler = new HttpStatusServerAccessDeniedHandler(HttpStatus.FORBIDDEN); private ServerAccessDeniedHandler accessDeniedHandler = new HttpStatusServerAccessDeniedHandler(HttpStatus.FORBIDDEN);
public void setServerAccessDeniedHandler( public void setAccessDeniedHandler(
ServerAccessDeniedHandler serverAccessDeniedHandler) { ServerAccessDeniedHandler accessDeniedHandler) {
Assert.notNull(serverAccessDeniedHandler, "serverAccessDeniedHandler"); Assert.notNull(accessDeniedHandler, "accessDeniedHandler");
this.serverAccessDeniedHandler = serverAccessDeniedHandler; this.accessDeniedHandler = accessDeniedHandler;
} }
public void setServerCsrfTokenRepository( public void setServerCsrfTokenRepository(
@ -85,7 +85,8 @@ public class CsrfWebFilter implements WebFilter {
.flatMap(m -> validateToken(exchange)) .flatMap(m -> validateToken(exchange))
.flatMap(m -> continueFilterChain(exchange, chain)) .flatMap(m -> continueFilterChain(exchange, chain))
.switchIfEmpty(continueFilterChain(exchange, chain).then(Mono.empty())) .switchIfEmpty(continueFilterChain(exchange, chain).then(Mono.empty()))
.onErrorResume(CsrfException.class, e -> this.serverAccessDeniedHandler.handle(exchange, e)); .onErrorResume(CsrfException.class, e -> this.accessDeniedHandler
.handle(exchange, e));
} }
private Mono<Void> validateToken(ServerWebExchange exchange) { private Mono<Void> validateToken(ServerWebExchange exchange) {

View File

@ -67,7 +67,7 @@ public class ExceptionTranslationWebFilterTests {
when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointPublisher.mono()); when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointPublisher.mono());
this.filter.setAuthenticationEntryPoint(this.entryPoint); this.filter.setAuthenticationEntryPoint(this.entryPoint);
this.filter.setServerAccessDeniedHandler(this.deniedHandler); this.filter.setAccessDeniedHandler(this.deniedHandler);
} }
@Test @Test
@ -150,7 +150,7 @@ public class ExceptionTranslationWebFilterTests {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void setAccessDeniedHandlerWhenNullThenException() { public void setAccessDeniedHandlerWhenNullThenException() {
this.filter.setServerAccessDeniedHandler(null); this.filter.setAccessDeniedHandler(null);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)