1
0
mirror of synced 2026-05-22 21:33:16 +00:00

Polish ExchangeMutatorWebFilter Support

Issue gh-4343
This commit is contained in:
Rob Winch
2017-05-30 22:44:56 -05:00
parent 6aa7f05579
commit 915de03f42
3 changed files with 19 additions and 20 deletions
@@ -161,16 +161,17 @@ public class HelloWebfluxApplicationTests {
@Test @Test
public void mockSupport() throws Exception { public void mockSupport() throws Exception {
ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser()); ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter();
WebTestClient mockRest = WebTestClient.bindToApplicationContext(this.context).webFilter(exchangeMutator).build(); WebTestClient mockRest = WebTestClient.bindToApplicationContext(this.context).webFilter(exchangeMutator).build();
mockRest mockRest
.filter(exchangeMutator.perClient(withUser()))
.get() .get()
.uri("/principal") .uri("/principal")
.exchange() .exchange()
.expectStatus().isOk(); .expectStatus().isOk();
this.rest mockRest
.get() .get()
.uri("/principal") .uri("/principal")
.exchange() .exchange()
@@ -167,16 +167,17 @@ public class HelloWebfluxFnApplicationTests {
@Test @Test
public void mockSupport() throws Exception { public void mockSupport() throws Exception {
ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser()); ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter();
WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator).build(); WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator, springSecurityFilterChain).build();
mockRest mockRest
.filter(exchangeMutator.perClient(withUser()))
.get() .get()
.uri("/principal") .uri("/principal")
.exchange() .exchange()
.expectStatus().isOk(); .expectStatus().isOk();
this.rest mockRest
.get() .get()
.uri("/principal") .uri("/principal")
.exchange() .exchange()
@@ -28,11 +28,13 @@ import reactor.core.publisher.Mono;
import java.security.Principal; import java.security.Principal;
import java.util.Collection; import java.util.Collection;
import java.util.function.Function;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
/** /**
* Test utilities for working with Spring Security and * 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 * @author Rob Winch
* @since 5.0 * @since 5.0
@@ -42,10 +44,9 @@ public class SecurityExchangeMutators {
* Updates the ServerWebExchange to use the provided Principal * Updates the ServerWebExchange to use the provided Principal
* *
* @param principal the principal to use. * @param principal the principal to use.
* @return the {@link UnaryOperator<ServerWebExchange>}} to provide to * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
*/ */
public static UnaryOperator<ServerWebExchange> withPrincipal(Principal principal) { public static Function<ServerWebExchange, ServerWebExchange> withPrincipal(Principal principal) {
return m -> m.mutate().principal(Mono.just(principal)).build(); 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 * Updates the ServerWebExchange to use the provided Authentication as the Principal
* *
* @param authentication the Authentication to use. * @param authentication the Authentication to use.
* @return the {@link UnaryOperator<ServerWebExchange>}} to provide to * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
*/ */
public static UnaryOperator<ServerWebExchange> withAuthentication(Authentication authentication) { public static Function<ServerWebExchange, ServerWebExchange> withAuthentication(Authentication authentication) {
return withPrincipal(authentication); return withPrincipal(authentication);
} }
@@ -65,10 +65,9 @@ public class SecurityExchangeMutators {
* the Principal * the Principal
* *
* @param userDetails the UserDetails to use. * @param userDetails the UserDetails to use.
* @return the {@link UnaryOperator<ServerWebExchange>}} to provide to * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
*/ */
public static UnaryOperator<ServerWebExchange> withUser(UserDetails userDetails) { public static Function<ServerWebExchange, ServerWebExchange> withUser(UserDetails userDetails) {
return withAuthentication(new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities())); 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 * the Principal. This uses a default username of "user", password of "password", and granted authorities of
* "ROLE_USER". * "ROLE_USER".
* *
* @return the {@link UnaryOperator<ServerWebExchange>}} to provide to * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
*/ */
public static UserExchangeMutator withUser() { public static UserExchangeMutator withUser() {
return withUser("user"); return withUser("user");
@@ -90,8 +88,7 @@ public class SecurityExchangeMutators {
* the Principal. This uses a default password of "password" and granted authorities of * the Principal. This uses a default password of "password" and granted authorities of
* "ROLE_USER". * "ROLE_USER".
* *
* @return the {@link UnaryOperator<ServerWebExchange>}} to provide to * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
*/ */
public static UserExchangeMutator withUser(String username) { public static UserExchangeMutator withUser(String username) {
return new UserExchangeMutator(username); return new UserExchangeMutator(username);
@@ -101,7 +98,7 @@ public class SecurityExchangeMutators {
* Updates the WebServerExchange using {@code SecurityExchangeMutators#withUser(UserDetails)}. Defaults to use a * Updates the WebServerExchange using {@code SecurityExchangeMutators#withUser(UserDetails)}. Defaults to use a
* password of "password" and granted authorities of "ROLE_USER". * password of "password" and granted authorities of "ROLE_USER".
*/ */
public static class UserExchangeMutator implements UnaryOperator<ServerWebExchange> { public static class UserExchangeMutator implements Function<ServerWebExchange, ServerWebExchange> {
private final User.UserBuilder userBuilder; private final User.UserBuilder userBuilder;
private UserExchangeMutator(String username) { private UserExchangeMutator(String username) {