Improve reactive support for access to Principal
The method to access the Principal from the ServerWebExchange is now a Mono<Principal> (rather than Optional<Principal>). There is also support for Principal as a controller method argument. Issue: SPR-14680, SPR-14865
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
package org.springframework.web.server;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -40,7 +39,7 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
private ServerHttpResponse response;
|
||||
|
||||
private Principal user;
|
||||
private Mono<Principal> user;
|
||||
|
||||
private Mono<WebSession> session;
|
||||
|
||||
@@ -66,7 +65,7 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerWebExchange.MutativeBuilder setPrincipal(Principal user) {
|
||||
public ServerWebExchange.MutativeBuilder setPrincipal(Mono<Principal> user) {
|
||||
this.user = user;
|
||||
return this;
|
||||
}
|
||||
@@ -100,7 +99,7 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
private final ServerHttpResponse response;
|
||||
|
||||
private final Principal user;
|
||||
private final Mono<Principal> userMono;
|
||||
|
||||
private final Mono<WebSession> session;
|
||||
|
||||
@@ -108,13 +107,13 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
|
||||
public MutativeDecorator(ServerWebExchange delegate,
|
||||
ServerHttpRequest request, ServerHttpResponse response, Principal user,
|
||||
ServerHttpRequest request, ServerHttpResponse response, Mono<Principal> user,
|
||||
Mono<WebSession> session, Mono<MultiValueMap<String, String>> formData) {
|
||||
|
||||
super(delegate);
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.user = user;
|
||||
this.userMono = user;
|
||||
this.session = session;
|
||||
this.formData = formData;
|
||||
}
|
||||
@@ -137,8 +136,8 @@ class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.Mutat
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T extends Principal> Optional<T> getPrincipal() {
|
||||
return (this.user != null ? Optional.of((T) this.user) : getDelegate().getPrincipal());
|
||||
public <T extends Principal> Mono<T> getPrincipal() {
|
||||
return (this.userMono != null ? (Mono<T>) this.userMono : getDelegate().getPrincipal());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -73,7 +73,7 @@ public interface ServerWebExchange {
|
||||
/**
|
||||
* Return the authenticated user for the request, if any.
|
||||
*/
|
||||
<T extends Principal> Optional<T> getPrincipal();
|
||||
<T extends Principal> Mono<T> getPrincipal();
|
||||
|
||||
/**
|
||||
* Return the form data from the body of the request or an empty {@code Mono}
|
||||
@@ -155,7 +155,7 @@ public interface ServerWebExchange {
|
||||
/**
|
||||
* Set the principal to use.
|
||||
*/
|
||||
MutativeBuilder setPrincipal(Principal user);
|
||||
MutativeBuilder setPrincipal(Mono<Principal> user);
|
||||
|
||||
/**
|
||||
* Set the session to use.
|
||||
|
||||
@@ -84,7 +84,7 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Principal> Optional<T> getPrincipal() {
|
||||
public <T extends Principal> Mono<T> getPrincipal() {
|
||||
return getDelegate().getPrincipal();
|
||||
}
|
||||
|
||||
|
||||
@@ -135,8 +135,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Principal> Optional<T> getPrincipal() {
|
||||
return Optional.empty();
|
||||
public <T extends Principal> Mono<T> getPrincipal() {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user