|
@@ -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
|
|
|
|
- * {@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();
|
|
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
|
|
|
|
- * {@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);
|
|
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
|
|
|
|
- * {@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()));
|
|
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
|
|
|
|
- * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
|
|
|
|
|
+ * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
|
|
*/
|
|
*/
|
|
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
|
|
|
|
- * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
|
|
|
|
|
|
+ * @return the {@link Function<ServerWebExchange, ServerWebExchange>}} to use
|
|
*/
|
|
*/
|
|
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) {
|