From a6bed9a1aa5ed301205f7c713442c9ee51326bce Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 7 Sep 2017 14:11:04 -0500 Subject: [PATCH] AuthenticationSuccessHandler uses WebFilterExchange Issue gh-4532 --- .../AuthenticationSuccessHandler.java | 5 ++--- .../authentication/AuthenticationWebFilter.java | 3 ++- .../RedirectAuthenticationSuccessHandler.java | 5 +++-- .../WebFilterChainAuthenticationSuccessHandler.java | 6 ++++-- .../AuthenticationWebFilterTests.java | 6 +++--- .../RedirectAuthenticationSuccessHandlerTests.java | 13 +++++++------ 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationSuccessHandler.java b/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationSuccessHandler.java index 6761115c15..b5b7bc4163 100644 --- a/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationSuccessHandler.java +++ b/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationSuccessHandler.java @@ -19,8 +19,7 @@ package org.springframework.security.web.server.authentication; import org.springframework.security.core.Authentication; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebFilterChain; +import org.springframework.security.web.server.WebFilterExchange; import reactor.core.publisher.Mono; /** @@ -28,5 +27,5 @@ import reactor.core.publisher.Mono; * @since 5.0 */ public interface AuthenticationSuccessHandler { - Mono success(Authentication authentication, ServerWebExchange exchange, WebFilterChain chain); + Mono success(Authentication authentication, WebFilterExchange webFilterExchange); } diff --git a/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationWebFilter.java b/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationWebFilter.java index c2b6a945d3..f5905d46a2 100644 --- a/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationWebFilter.java +++ b/webflux/src/main/java/org/springframework/security/web/server/authentication/AuthenticationWebFilter.java @@ -27,6 +27,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.security.web.server.AuthenticationEntryPoint; import org.springframework.security.web.server.HttpBasicAuthenticationConverter; +import org.springframework.security.web.server.WebFilterExchange; import org.springframework.security.web.server.authentication.www.HttpBasicAuthenticationEntryPoint; import org.springframework.security.web.server.context.SecurityContextRepository; import org.springframework.security.web.server.context.SecurityContextRepositoryServerWebExchange; @@ -87,7 +88,7 @@ public class AuthenticationWebFilter implements WebFilter { SecurityContextImpl securityContext = new SecurityContextImpl(); securityContext.setAuthentication(authentication); return this.securityContextRepository.save(exchange, securityContext) - .then(this.authenticationSuccessHandler.success(authentication, exchange, chain)); + .then(this.authenticationSuccessHandler.success(authentication, new WebFilterExchange(exchange, chain))); } public void setSecurityContextRepository( diff --git a/webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandler.java b/webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandler.java index e572207427..73de5bc1c9 100644 --- a/webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandler.java +++ b/webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandler.java @@ -21,6 +21,7 @@ package org.springframework.security.web.server.authentication; import org.springframework.security.core.Authentication; import org.springframework.security.web.server.DefaultRedirectStrategy; import org.springframework.security.web.server.RedirectStrategy; +import org.springframework.security.web.server.WebFilterExchange; import org.springframework.util.Assert; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilterChain; @@ -38,8 +39,8 @@ public class RedirectAuthenticationSuccessHandler implements AuthenticationSucce private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); @Override - public Mono success(Authentication authentication, - ServerWebExchange exchange, WebFilterChain chain) { + public Mono success(Authentication authentication, WebFilterExchange webFilterExchange) { + ServerWebExchange exchange = webFilterExchange.getExchange(); return this.redirectStrategy.sendRedirect(exchange, this.location); } diff --git a/webflux/src/main/java/org/springframework/security/web/server/authentication/WebFilterChainAuthenticationSuccessHandler.java b/webflux/src/main/java/org/springframework/security/web/server/authentication/WebFilterChainAuthenticationSuccessHandler.java index e56f11688c..52f67a6db6 100644 --- a/webflux/src/main/java/org/springframework/security/web/server/authentication/WebFilterChainAuthenticationSuccessHandler.java +++ b/webflux/src/main/java/org/springframework/security/web/server/authentication/WebFilterChainAuthenticationSuccessHandler.java @@ -19,6 +19,7 @@ package org.springframework.security.web.server.authentication; import org.springframework.security.core.Authentication; +import org.springframework.security.web.server.WebFilterExchange; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; @@ -29,7 +30,8 @@ import reactor.core.publisher.Mono; */ public class WebFilterChainAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Override - public Mono success(Authentication authentication, ServerWebExchange exchange, WebFilterChain chain) { - return chain.filter(exchange); + public Mono success(Authentication authentication, WebFilterExchange webFilterExchange) { + ServerWebExchange exchange = webFilterExchange.getExchange(); + return webFilterExchange.getChain().filter(exchange); } } diff --git a/webflux/src/test/java/org/springframework/security/web/server/authentication/AuthenticationWebFilterTests.java b/webflux/src/test/java/org/springframework/security/web/server/authentication/AuthenticationWebFilterTests.java index 2abfd9659a..fc675db61c 100644 --- a/webflux/src/test/java/org/springframework/security/web/server/authentication/AuthenticationWebFilterTests.java +++ b/webflux/src/test/java/org/springframework/security/web/server/authentication/AuthenticationWebFilterTests.java @@ -150,7 +150,7 @@ public class AuthenticationWebFilterTests { .bindToWebFilters(this.filter) .build(); - EntityExchangeResult result = client + client .get() .uri("/") .exchange() @@ -188,7 +188,7 @@ public class AuthenticationWebFilterTests { Mono authentication = Mono.just(new TestingAuthenticationToken("test", "this", "ROLE_USER")); when(this.authenticationConverter.apply(any())).thenReturn(authentication); when(this.authenticationManager.authenticate(any())).thenReturn(authentication); - when(this.successHandler.success(any(),any(),any())).thenReturn(Mono.empty()); + when(this.successHandler.success(any(),any())).thenReturn(Mono.empty()); when(this.securityContextRepository.save(any(),any())).thenAnswer( a -> Mono.just(a.getArguments()[0])); WebTestClient client = WebTestClientBuilder @@ -202,7 +202,7 @@ public class AuthenticationWebFilterTests { .expectStatus().isOk() .expectBody().isEmpty(); - verify(this.successHandler).success(eq(authentication.block()), any(), any()); + verify(this.successHandler).success(eq(authentication.block()), any()); verify(this.securityContextRepository).save(any(), any()); verifyZeroInteractions(this.entryPoint); } diff --git a/webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandlerTests.java b/webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandlerTests.java index 319d209408..09244249f6 100644 --- a/webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandlerTests.java +++ b/webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationSuccessHandlerTests.java @@ -28,6 +28,7 @@ import org.springframework.security.authentication.AuthenticationCredentialsNotF import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.server.RedirectStrategy; +import org.springframework.security.web.server.WebFilterExchange; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; @@ -70,8 +71,8 @@ public class RedirectAuthenticationSuccessHandlerTests { @Test public void successWhenNoSubscribersThenNoActions() { - this.handler.success(this.authentication, this.exchange, - this.chain); + this.handler.success(this.authentication, new WebFilterExchange(this.exchange, + this.chain)); verifyZeroInteractions(this.exchange); } @@ -80,8 +81,8 @@ public class RedirectAuthenticationSuccessHandlerTests { public void successWhenSubscribeThenStatusAndLocationSet() { this.exchange = MockServerHttpRequest.get("/").toExchange(); - this.handler.success(this.authentication, this.exchange, - this.chain).block(); + this.handler.success(this.authentication, new WebFilterExchange(this.exchange, + this.chain)).block(); assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo( HttpStatus.FOUND); @@ -95,8 +96,8 @@ public class RedirectAuthenticationSuccessHandlerTests { this.handler.setRedirectStrategy(this.redirectStrategy); this.exchange = MockServerHttpRequest.get("/").toExchange(); - assertThat(this.handler.success(this.authentication, this.exchange, - this.chain)).isEqualTo(result); + assertThat(this.handler.success(this.authentication, new WebFilterExchange(this.exchange, + this.chain))).isEqualTo(result); verify(this.redirectStrategy).sendRedirect(any(), eq(this.location)); }