Polish ExchangeMutatorWebFilter Support
Issue gh-4343
This commit is contained in:
+12
-15
@@ -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<ServerWebExchange>}} to provide to
|
||||
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
||||
* @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
|
||||
*/
|
||||
public static UnaryOperator<ServerWebExchange> withPrincipal(Principal principal) {
|
||||
public static Function<ServerWebExchange, ServerWebExchange> 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<ServerWebExchange>}} to provide to
|
||||
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
||||
* @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
|
||||
*/
|
||||
public static UnaryOperator<ServerWebExchange> withAuthentication(Authentication authentication) {
|
||||
public static Function<ServerWebExchange, ServerWebExchange> 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<ServerWebExchange>}} to provide to
|
||||
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
||||
* @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
|
||||
*/
|
||||
public static UnaryOperator<ServerWebExchange> withUser(UserDetails userDetails) {
|
||||
public static Function<ServerWebExchange, ServerWebExchange> 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<ServerWebExchange>}} to provide to
|
||||
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
||||
* @return the {@link Function<ServerWebExchange, ServerWebExchange>}} 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<ServerWebExchange>}} to provide to
|
||||
* {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
||||
* @return the {@link Function<ServerWebExchange, ServerWebExchange>}} 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<ServerWebExchange> {
|
||||
public static class UserExchangeMutator implements Function<ServerWebExchange, ServerWebExchange> {
|
||||
private final User.UserBuilder userBuilder;
|
||||
|
||||
private UserExchangeMutator(String username) {
|
||||
|
||||
Reference in New Issue
Block a user