Provide EndpointRequest for WebFlux-based Security

Closes gh-11022
This commit is contained in:
Madhura Bhave
2018-01-09 11:26:26 -08:00
parent 32557e4987
commit e57aafd63d
14 changed files with 785 additions and 30 deletions

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2012-2018 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.boot.security.reactive;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
/**
* {@link ApplicationContext} backed {@link ServerWebExchangeMatcher}. Can work directly with the
* {@link ApplicationContext}, obtain an existing bean or
* {@link AutowireCapableBeanFactory#createBean(Class, int, boolean) create a new bean}
* that is autowired in the usual way.
*
* @param <C> The type of the context that the match method actually needs to use. Can be
* an {@link ApplicationContext}, a class of an {@link ApplicationContext#getBean(Class)
* existing bean} or a custom type that will be
* {@link AutowireCapableBeanFactory#createBean(Class, int, boolean) created} on demand.
* @author Madhura Bhave
* @since 2.0.0
*/
public abstract class ApplicationContextServerWebExchangeMatcher<C> implements ServerWebExchangeMatcher {
private final Class<? extends C> contextClass;
private C context;
private Object contextLock = new Object();
public ApplicationContextServerWebExchangeMatcher(Class<? extends C> contextClass) {
Assert.notNull(contextClass, "Context class must not be null");
this.contextClass = contextClass;
}
@Override
public final Mono<MatchResult> matches(ServerWebExchange exchange) {
return matches(exchange, getContext(exchange));
}
/**
* Decides whether the rule implemented by the strategy matches the supplied exchange.
* @param exchange the source exchange
* @param context the context instance
* @return if the exchange matches
*/
protected abstract Mono<MatchResult> matches(ServerWebExchange exchange, C context);
protected C getContext(ServerWebExchange exchange) {
if (this.context == null) {
synchronized (this.contextLock) {
this.context = createContext(exchange);
initialized(this.context);
}
}
return this.context;
}
/**
* Called once the context has been initialized.
* @param context the initialized context
*/
protected void initialized(C context) {
}
@SuppressWarnings("unchecked")
private C createContext(ServerWebExchange exchange) {
ApplicationContext context = exchange.getApplicationContext();
if (context == null) {
throw new IllegalStateException("No WebApplicationContext found.");
}
if (this.contextClass.isInstance(context)) {
return (C) context;
}
try {
return context.getBean(this.contextClass);
}
catch (NoSuchBeanDefinitionException ex) {
return (C) context.getAutowireCapableBeanFactory().createBean(
this.contextClass, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR,
false);
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.security;
package org.springframework.boot.security.servlet;
import javax.servlet.http.HttpServletRequest;
@@ -53,7 +53,7 @@ public abstract class ApplicationContextRequestMatcher<C> implements RequestMatc
}
@Override
public boolean matches(HttpServletRequest request) {
public final boolean matches(HttpServletRequest request) {
return matches(request, getContext(request));
}

View File

@@ -0,0 +1,157 @@
/*
* Copyright 2012-2017 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.boot.security.reactive;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import reactor.core.publisher.Mono;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link ApplicationContextServerWebExchangeMatcher}.
*
* @author Madhura Bhave
*/
public class ApplicationContextServerWebExchangeMatcherTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createWhenContextClassIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Context class must not be null");
new TestApplicationContextServerWebExchangeMatcher<>(null);
}
@Test
public void matchesWhenContextClassIsApplicationContextShouldProvideContext() {
ServerWebExchange exchange = createHttpWebHandlerAdapter();
StaticApplicationContext context = (StaticApplicationContext) exchange.getApplicationContext();
assertThat(new TestApplicationContextServerWebExchangeMatcher<>(ApplicationContext.class)
.callMatchesAndReturnProvidedContext(exchange)).isEqualTo(context);
}
@Test
public void matchesWhenContextClassIsExistingBeanShouldProvideBean() {
ServerWebExchange exchange = createHttpWebHandlerAdapter();
StaticApplicationContext context = (StaticApplicationContext) exchange.getApplicationContext();
context.registerSingleton("existingBean", ExistingBean.class);
assertThat(new TestApplicationContextServerWebExchangeMatcher<>(ExistingBean.class)
.callMatchesAndReturnProvidedContext(exchange))
.isEqualTo(context.getBean(ExistingBean.class));
}
@Test
public void matchesWhenContextClassIsNewBeanShouldProvideBean() {
ServerWebExchange exchange = createHttpWebHandlerAdapter();
StaticApplicationContext context = (StaticApplicationContext) exchange.getApplicationContext();
context.registerSingleton("existingBean", ExistingBean.class);
assertThat(new TestApplicationContextServerWebExchangeMatcher<>(NewBean.class)
.callMatchesAndReturnProvidedContext(exchange).getBean())
.isEqualTo(context.getBean(ExistingBean.class));
}
@Test
public void matchesWhenContextIsNull() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path").build());
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("No WebApplicationContext found.");
new TestApplicationContextServerWebExchangeMatcher<>(ExistingBean.class)
.callMatchesAndReturnProvidedContext(exchange);
}
private ServerWebExchange createHttpWebHandlerAdapter() {
StaticApplicationContext context = new StaticApplicationContext();
TestHttpWebHandlerAdapter adapter = new TestHttpWebHandlerAdapter(mock(WebHandler.class));
adapter.setApplicationContext(context);
return adapter.createExchange(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse());
}
static class TestHttpWebHandlerAdapter extends HttpWebHandlerAdapter {
TestHttpWebHandlerAdapter(WebHandler delegate) {
super(delegate);
}
@Override
protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttpResponse response) {
return super.createExchange(request, response);
}
}
static class ExistingBean {
}
static class NewBean {
private final ExistingBean bean;
NewBean(ExistingBean bean) {
this.bean = bean;
}
public ExistingBean getBean() {
return this.bean;
}
}
static class TestApplicationContextServerWebExchangeMatcher<C>
extends ApplicationContextServerWebExchangeMatcher<C> {
private C providedContext;
TestApplicationContextServerWebExchangeMatcher(Class<? extends C> context) {
super(context);
}
C callMatchesAndReturnProvidedContext(ServerWebExchange exchange) {
matches(exchange);
return getProvidedContext();
}
@Override
protected Mono<MatchResult> matches(ServerWebExchange exchange, C context) {
this.providedContext = context;
return MatchResult.match();
}
C getProvidedContext() {
return this.providedContext;
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.security;
package org.springframework.boot.security.servlet;
import javax.servlet.http.HttpServletRequest;