Flux member variables in favor of Collections

Fix gh-4694
This commit is contained in:
Rob Winch 2017-10-24 20:26:02 -05:00
parent 3b85512e48
commit 44b41e78cd
8 changed files with 24 additions and 29 deletions

View File

@ -50,7 +50,7 @@ public class WebFluxSecurityConfiguration {
@Bean(SPRING_SECURITY_WEBFILTERCHAINFILTER_BEAN_NAME)
@Order(value = WEB_FILTER_CHAIN_FILTER_ORDER)
public WebFilterChainProxy springSecurityWebFilterChainFilter() {
return WebFilterChainProxy.fromSecurityWebFilterChainsList(getSecurityWebFilterChains());
return new WebFilterChainProxy(getSecurityWebFilterChains());
}
private List<SecurityWebFilterChain> getSecurityWebFilterChains() {

View File

@ -99,7 +99,7 @@ public class FormLoginTests {
WebTestClient webTestClient = WebTestClient
.bindToController(new CustomLoginPageController(), new WebTestClientBuilder.Http200RestController())
.webFilter(WebFilterChainProxy.fromSecurityWebFilterChains(securityWebFilter))
.webFilter(new WebFilterChainProxy(securityWebFilter))
.build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder

View File

@ -118,7 +118,7 @@ public class ServerHttpSecurityTests {
}
private WebTestClient buildClient() {
WebFilterChainProxy springSecurityFilterChain = WebFilterChainProxy.fromSecurityWebFilterChains(
WebFilterChainProxy springSecurityFilterChain = new WebFilterChainProxy(
this.http.build());
return WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build();
}

View File

@ -17,6 +17,7 @@
package org.springframework.security.web.server;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -33,7 +34,7 @@ import java.util.List;
*/
public class DelegatingServerAuthenticationEntryPoint
implements ServerAuthenticationEntryPoint {
private final Flux<DelegateEntry> entryPoints;
private final List<DelegateEntry> entryPoints;
private ServerAuthenticationEntryPoint defaultEntryPoint = (exchange, e) -> {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
@ -47,12 +48,14 @@ public class DelegatingServerAuthenticationEntryPoint
public DelegatingServerAuthenticationEntryPoint(
List<DelegateEntry> entryPoints) {
this.entryPoints = Flux.fromIterable(entryPoints);
Assert.notEmpty(entryPoints, "entryPoints cannot be null");
this.entryPoints = entryPoints;
}
public Mono<Void> commence(ServerWebExchange exchange,
AuthenticationException e) {
return this.entryPoints.filterWhen( entry -> isMatch(exchange, entry))
return Flux.fromIterable(this.entryPoints)
.filterWhen( entry -> isMatch(exchange, entry))
.next()
.map( entry -> entry.getEntryPoint())
.defaultIfEmpty(this.defaultEntryPoint)

View File

@ -17,6 +17,7 @@
package org.springframework.security.web.server;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Flux;
@ -30,13 +31,11 @@ import java.util.List;
*/
public class MatcherSecurityWebFilterChain implements SecurityWebFilterChain {
private final ServerWebExchangeMatcher matcher;
private final Flux<WebFilter> filters;
private final List<WebFilter> filters;
public MatcherSecurityWebFilterChain(ServerWebExchangeMatcher matcher, List<WebFilter> filters) {
this(matcher, Flux.fromIterable(filters));
}
public MatcherSecurityWebFilterChain(ServerWebExchangeMatcher matcher, Flux<WebFilter> filters) {
Assert.notNull(matcher, "matcher cannot be null");
Assert.notEmpty(filters, "filters cannot be null or empty. Got " + filters);
this.matcher = matcher;
this.filters = filters;
}
@ -49,6 +48,6 @@ public class MatcherSecurityWebFilterChain implements SecurityWebFilterChain {
@Override
public Flux<WebFilter> getWebFilters() {
return filters;
return Flux.fromIterable(this.filters);
}
}

View File

@ -15,6 +15,7 @@
*/
package org.springframework.security.web.server;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -33,15 +34,19 @@ import reactor.core.publisher.Mono;
* @since 5.0
*/
public class WebFilterChainProxy implements WebFilter {
private final Flux<SecurityWebFilterChain> filters;
private final List<SecurityWebFilterChain> filters;
public WebFilterChainProxy(Flux<SecurityWebFilterChain> filters) {
public WebFilterChainProxy(List<SecurityWebFilterChain> filters) {
this.filters = filters;
}
public WebFilterChainProxy(SecurityWebFilterChain... filters) {
this.filters = Arrays.asList(filters);
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return filters
return Flux.fromIterable(this.filters)
.filterWhen( securityWebFilterChain -> securityWebFilterChain.matches(exchange))
.next()
.switchIfEmpty(chain.filter(exchange).then(Mono.empty()))
@ -52,16 +57,4 @@ public class WebFilterChainProxy implements WebFilter {
.map( handler -> new DefaultWebFilterChain(handler) )
.flatMap( securedChain -> securedChain.filter(exchange));
}
public static WebFilterChainProxy fromWebFiltersList(List<WebFilter> filters) {
return new WebFilterChainProxy(Flux.just(new MatcherSecurityWebFilterChain(ServerWebExchangeMatchers.anyExchange(), filters)));
}
public static WebFilterChainProxy fromSecurityWebFilterChainsList(List<SecurityWebFilterChain> securityWebFilterChains) {
return new WebFilterChainProxy(Flux.fromIterable(securityWebFilterChains));
}
public static WebFilterChainProxy fromSecurityWebFilterChains(SecurityWebFilterChain... securityWebFilterChains) {
return fromSecurityWebFilterChainsList(Arrays.asList(securityWebFilterChains));
}
}

View File

@ -40,7 +40,7 @@ public class WebTestClientBuilder {
}
public static Builder bindToWebFilters(SecurityWebFilterChain securityWebFilterChain) {
return bindToWebFilters(WebFilterChainProxy.fromSecurityWebFilterChains(securityWebFilterChain));
return bindToWebFilters(new WebFilterChainProxy(securityWebFilterChain));
}
@RestController

View File

@ -42,7 +42,7 @@ public class WebFilterChainProxyTests {
List<WebFilter> filters = Arrays.asList(new Http200WebFilter());
ServerWebExchangeMatcher notMatch = exchange -> MatchResult.notMatch();
MatcherSecurityWebFilterChain chain = new MatcherSecurityWebFilterChain(notMatch, filters);
WebFilterChainProxy filter = WebFilterChainProxy.fromSecurityWebFilterChains(chain);
WebFilterChainProxy filter = new WebFilterChainProxy(chain);
WebTestClient.bindToController(new Object()).webFilter(filter).build()
.get()