From ec1eb14280e2a5b4584504938d657c63c938a0d0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 17 Oct 2016 17:48:22 -0400 Subject: [PATCH] Add getPrincipal to ServerWebExchange ServerWebExchange now has a getPrincipal method and along with that a ServerWebExchangeDecorator that can be used to wrap the exchange in order to return the authenticated user. Issue: SPR-14680 --- .../web/server/ServerWebExchange.java | 6 + .../server/ServerWebExchangeDecorator.java | 109 ++++++++++++++++++ .../adapter/DefaultServerWebExchange.java | 6 + 3 files changed, 121 insertions(+) create mode 100644 spring-web/src/main/java/org/springframework/web/server/ServerWebExchangeDecorator.java diff --git a/spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java index 5789bcebed..3a48d41cba 100644 --- a/spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java @@ -16,6 +16,7 @@ package org.springframework.web.server; +import java.security.Principal; import java.time.Instant; import java.util.Map; import java.util.Optional; @@ -68,6 +69,11 @@ public interface ServerWebExchange { */ Mono getSession(); + /** + * Return the authenticated user for the request, if any. + */ + Optional getPrincipal(); + /** * Returns {@code true} if the one of the {@code checkNotModified} methods * in this contract were used and they returned true. diff --git a/spring-web/src/main/java/org/springframework/web/server/ServerWebExchangeDecorator.java b/spring-web/src/main/java/org/springframework/web/server/ServerWebExchangeDecorator.java new file mode 100644 index 0000000000..260511d112 --- /dev/null +++ b/spring-web/src/main/java/org/springframework/web/server/ServerWebExchangeDecorator.java @@ -0,0 +1,109 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.web.server; + +import java.security.Principal; +import java.time.Instant; +import java.util.Map; +import java.util.Optional; + +import reactor.core.publisher.Mono; + +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.util.Assert; + +/** + * Wraps another {@link ServerWebExchange} and delegates all methods to it. + * Sub-classes can override specific methods, e.g. {@link #getPrincipal()} to + * return the authenticated user for the request. + * + * @author Rossen Stoyanchev + */ +public class ServerWebExchangeDecorator implements ServerWebExchange { + + private final ServerWebExchange delegate; + + + public ServerWebExchangeDecorator(ServerWebExchange delegate) { + Assert.notNull(delegate, "'delegate' is required."); + this.delegate = delegate; + } + + + public ServerWebExchange getDelegate() { + return this.delegate; + } + + // ServerWebExchange delegation methods... + + @Override + public ServerHttpRequest getRequest() { + return this.getDelegate().getRequest(); + } + + @Override + public ServerHttpResponse getResponse() { + return this.getDelegate().getResponse(); + } + + @Override + public Map getAttributes() { + return this.getDelegate().getAttributes(); + } + + @Override + public Optional getAttribute(String name) { + return this.getDelegate().getAttribute(name); + } + + @Override + public Mono getSession() { + return this.getDelegate().getSession(); + } + + @Override + public Optional getPrincipal() { + return this.getDelegate().getPrincipal(); + } + + @Override + public boolean isNotModified() { + return this.getDelegate().isNotModified(); + } + + @Override + public boolean checkNotModified(Instant lastModified) { + return this.getDelegate().checkNotModified(lastModified); + } + + @Override + public boolean checkNotModified(String etag) { + return this.getDelegate().checkNotModified(etag); + } + + @Override + public boolean checkNotModified(String etag, Instant lastModified) { + return this.getDelegate().checkNotModified(etag, lastModified); + } + + + @Override + public String toString() { + return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]"; + } + +} diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index af75fea7e5..a78b68f792 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -16,6 +16,7 @@ package org.springframework.web.server.adapter; +import java.security.Principal; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Arrays; @@ -104,6 +105,11 @@ public class DefaultServerWebExchange implements ServerWebExchange { return this.sessionMono; } + @Override + public Optional getPrincipal() { + return Optional.empty(); + } + @Override public boolean isNotModified() { return this.notModified;