Expose Principal in ServerRequest
This commit exposes the Principal in ServerRequest. Issue: SPR-15552
This commit is contained in:
parent
94efbe2687
commit
ba39697f2e
|
|
@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
|
@ -157,6 +158,11 @@ class DefaultServerRequest implements ServerRequest {
|
|||
return this.exchange.getSession();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<? extends Principal> principal() {
|
||||
return this.exchange.getPrincipal();
|
||||
}
|
||||
|
||||
private ServerHttpRequest request() {
|
||||
return this.exchange.getRequest();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.net.URI;
|
||||
import java.security.Principal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
|
@ -543,6 +544,11 @@ public abstract class RequestPredicates {
|
|||
return this.request.session();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<? extends Principal> principal() {
|
||||
return this.request.principal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return method() + " " + path();
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
|
@ -168,6 +169,11 @@ public interface ServerRequest {
|
|||
*/
|
||||
Mono<WebSession> session();
|
||||
|
||||
/**
|
||||
* Return the authenticated user for the request, if any.
|
||||
*/
|
||||
Mono<? extends Principal> principal();
|
||||
|
||||
|
||||
/**
|
||||
* Represents the headers of the HTTP request.
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server.support;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
|
@ -144,6 +145,10 @@ public class ServerRequestWrapper implements ServerRequest {
|
|||
return this.delegate.session();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<? extends Principal> principal() {
|
||||
return this.delegate.principal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of the {@code Headers} interface that can be subclassed
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
|
@ -66,11 +67,12 @@ public class MockServerRequest implements ServerRequest {
|
|||
|
||||
private final WebSession session;
|
||||
|
||||
private Principal principal;
|
||||
|
||||
private MockServerRequest(HttpMethod method, URI uri,
|
||||
MockHeaders headers, Object body, Map<String, Object> attributes,
|
||||
MultiValueMap<String, String> queryParams,
|
||||
Map<String, String> pathVariables, WebSession session) {
|
||||
Map<String, String> pathVariables, WebSession session, Principal principal) {
|
||||
|
||||
this.method = method;
|
||||
this.uri = uri;
|
||||
|
|
@ -80,6 +82,7 @@ public class MockServerRequest implements ServerRequest {
|
|||
this.queryParams = queryParams;
|
||||
this.pathVariables = pathVariables;
|
||||
this.session = session;
|
||||
this.principal = principal;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -148,6 +151,10 @@ public class MockServerRequest implements ServerRequest {
|
|||
return Mono.justOrEmpty(this.session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<? extends Principal> principal() {
|
||||
return Mono.justOrEmpty(this.principal);
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new BuilderImpl();
|
||||
|
|
@ -178,6 +185,8 @@ public class MockServerRequest implements ServerRequest {
|
|||
|
||||
Builder session(WebSession session);
|
||||
|
||||
Builder session(Principal principal);
|
||||
|
||||
MockServerRequest body(Object body);
|
||||
|
||||
MockServerRequest build();
|
||||
|
|
@ -202,6 +211,8 @@ public class MockServerRequest implements ServerRequest {
|
|||
|
||||
private WebSession session;
|
||||
|
||||
private Principal principal;
|
||||
|
||||
@Override
|
||||
public Builder method(HttpMethod method) {
|
||||
Assert.notNull(method, "'method' must not be null");
|
||||
|
|
@ -283,17 +294,26 @@ public class MockServerRequest implements ServerRequest {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder session(Principal principal) {
|
||||
Assert.notNull(principal, "'principal' must not be null");
|
||||
this.principal = principal;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerRequest body(Object body) {
|
||||
this.body = body;
|
||||
return new MockServerRequest(this.method, this.uri, this.headers, this.body,
|
||||
this.attributes, this.queryParams, this.pathVariables, this.session);
|
||||
this.attributes, this.queryParams, this.pathVariables, this.session,
|
||||
this.principal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerRequest build() {
|
||||
return new MockServerRequest(this.method, this.uri, this.headers, null,
|
||||
this.attributes, this.queryParams, this.pathVariables, this.session);
|
||||
this.attributes, this.queryParams, this.pathVariables, this.session,
|
||||
this.principal);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue