From 915de03f429bc61ee51d2df6087041ccfaf94ee6 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Tue, 30 May 2017 22:44:56 -0500 Subject: [PATCH] Polish ExchangeMutatorWebFilter Support Issue gh-4343 --- .../sample/HelloWebfluxApplicationTests.java | 5 ++-- .../HelloWebfluxFnApplicationTests.java | 7 ++--- .../server/SecurityExchangeMutators.java | 27 +++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/samples/javaconfig/hellowebflux/src/test/java/sample/HelloWebfluxApplicationTests.java b/samples/javaconfig/hellowebflux/src/test/java/sample/HelloWebfluxApplicationTests.java index f22a7d3485..e90ec06318 100644 --- a/samples/javaconfig/hellowebflux/src/test/java/sample/HelloWebfluxApplicationTests.java +++ b/samples/javaconfig/hellowebflux/src/test/java/sample/HelloWebfluxApplicationTests.java @@ -161,16 +161,17 @@ public class HelloWebfluxApplicationTests { @Test public void mockSupport() throws Exception { - ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser()); + ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(); WebTestClient mockRest = WebTestClient.bindToApplicationContext(this.context).webFilter(exchangeMutator).build(); mockRest + .filter(exchangeMutator.perClient(withUser())) .get() .uri("/principal") .exchange() .expectStatus().isOk(); - this.rest + mockRest .get() .uri("/principal") .exchange() diff --git a/samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTests.java b/samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTests.java index b182ab66d8..13e3c80529 100644 --- a/samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTests.java +++ b/samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTests.java @@ -167,16 +167,17 @@ public class HelloWebfluxFnApplicationTests { @Test public void mockSupport() throws Exception { - ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser()); - WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator).build(); + ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(); + WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator, springSecurityFilterChain).build(); mockRest + .filter(exchangeMutator.perClient(withUser())) .get() .uri("/principal") .exchange() .expectStatus().isOk(); - this.rest + mockRest .get() .uri("/principal") .exchange() diff --git a/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityExchangeMutators.java b/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityExchangeMutators.java index 7f40022aea..3023ce0d97 100644 --- a/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityExchangeMutators.java +++ b/test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityExchangeMutators.java @@ -28,11 +28,13 @@ import reactor.core.publisher.Mono; import java.security.Principal; import java.util.Collection; +import java.util.function.Function; import java.util.function.UnaryOperator; /** * Test utilities for working with Spring Security and - * {{@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}}. + * {{@link org.springframework.test.web.reactive.server.WebTestClient}} using + * {{{@link org.springframework.test.web.reactive.server.ExchangeMutatorWebFilter}}}. * * @author Rob Winch * @since 5.0 @@ -42,10 +44,9 @@ public class SecurityExchangeMutators { * Updates the ServerWebExchange to use the provided Principal * * @param principal the principal to use. - * @return the {@link UnaryOperator}} to provide to - * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)} + * @return the {@link Function}} to use */ - public static UnaryOperator withPrincipal(Principal principal) { + public static Function withPrincipal(Principal principal) { return m -> m.mutate().principal(Mono.just(principal)).build(); } @@ -53,10 +54,9 @@ public class SecurityExchangeMutators { * Updates the ServerWebExchange to use the provided Authentication as the Principal * * @param authentication the Authentication to use. - * @return the {@link UnaryOperator}} to provide to - * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)} + * @return the {@link Function}} to use */ - public static UnaryOperator withAuthentication(Authentication authentication) { + public static Function withAuthentication(Authentication authentication) { return withPrincipal(authentication); } @@ -65,10 +65,9 @@ public class SecurityExchangeMutators { * the Principal * * @param userDetails the UserDetails to use. - * @return the {@link UnaryOperator}} to provide to - * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)} + * @return the {@link Function}} to use */ - public static UnaryOperator withUser(UserDetails userDetails) { + public static Function withUser(UserDetails userDetails) { return withAuthentication(new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities())); } @@ -77,8 +76,7 @@ public class SecurityExchangeMutators { * the Principal. This uses a default username of "user", password of "password", and granted authorities of * "ROLE_USER". * - * @return the {@link UnaryOperator}} to provide to - * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)} + * @return the {@link Function}} to use */ public static UserExchangeMutator withUser() { return withUser("user"); @@ -90,8 +88,7 @@ public class SecurityExchangeMutators { * the Principal. This uses a default password of "password" and granted authorities of * "ROLE_USER". * - * @return the {@link UnaryOperator}} to provide to - * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)} + * @return the {@link Function}} to use */ public static UserExchangeMutator withUser(String username) { return new UserExchangeMutator(username); @@ -101,7 +98,7 @@ public class SecurityExchangeMutators { * Updates the WebServerExchange using {@code SecurityExchangeMutators#withUser(UserDetails)}. Defaults to use a * password of "password" and granted authorities of "ROLE_USER". */ - public static class UserExchangeMutator implements UnaryOperator { + public static class UserExchangeMutator implements Function { private final User.UserBuilder userBuilder; private UserExchangeMutator(String username) {