EnableWebFluxSecurityTests uses SpringTestRule
This will hopefully resolve the periodic failures in EnableWebFluxSecurityTests
This commit is contained in:
parent
9d46af3d7c
commit
d231441cc0
|
@ -16,9 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.security.config.annotation.web.reactive;
|
package org.springframework.security.config.annotation.web.reactive;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.experimental.runners.Enclosed;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
@ -26,18 +25,18 @@ import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||||
|
import org.springframework.security.config.test.SpringTestRule;
|
||||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
||||||
import org.springframework.security.core.userdetails.User;
|
|
||||||
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
|
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
||||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||||
import org.springframework.security.web.server.WebFilterChainProxy;
|
import org.springframework.security.web.server.WebFilterChainProxy;
|
||||||
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
|
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.FluxExchangeResult;
|
import org.springframework.test.web.reactive.server.FluxExchangeResult;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
@ -56,14 +55,17 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
@RunWith(Enclosed.class)
|
|
||||||
public class EnableWebFluxSecurityTests {
|
public class EnableWebFluxSecurityTests {
|
||||||
@RunWith(SpringRunner.class)
|
@Rule
|
||||||
public static class Defaults {
|
public final SpringTestRule spring = new SpringTestRule();
|
||||||
@Autowired WebFilterChainProxy springSecurityFilterChain;
|
|
||||||
|
@Autowired
|
||||||
|
WebFilterChainProxy springSecurityFilterChain;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultRequiresAuthentication() {
|
public void defaultRequiresAuthentication() {
|
||||||
|
this.spring.register(Config.class).autowire();
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(this.springSecurityFilterChain)
|
.bindToWebFilters(this.springSecurityFilterChain)
|
||||||
.build();
|
.build();
|
||||||
|
@ -77,6 +79,8 @@ public class EnableWebFluxSecurityTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void authenticateWhenBasicThenNoSession() {
|
public void authenticateWhenBasicThenNoSession() {
|
||||||
|
this.spring.register(Config.class).autowire();
|
||||||
|
|
||||||
WebTestClient client = WebTestClientBuilder
|
WebTestClient client = WebTestClientBuilder
|
||||||
.bindToWebFilters(this.springSecurityFilterChain)
|
.bindToWebFilters(this.springSecurityFilterChain)
|
||||||
.filter(basicAuthentication())
|
.filter(basicAuthentication())
|
||||||
|
@ -92,6 +96,7 @@ public class EnableWebFluxSecurityTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultPopulatesReactorContext() {
|
public void defaultPopulatesReactorContext() {
|
||||||
|
this.spring.register(Config.class).autowire();
|
||||||
Principal currentPrincipal = new TestingAuthenticationToken("user", "password", "ROLE_USER");
|
Principal currentPrincipal = new TestingAuthenticationToken("user", "password", "ROLE_USER");
|
||||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
||||||
(exchange, chain) ->
|
(exchange, chain) ->
|
||||||
|
@ -114,6 +119,7 @@ public class EnableWebFluxSecurityTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultPopulatesReactorContextWhenAuthenticating() {
|
public void defaultPopulatesReactorContextWhenAuthenticating() {
|
||||||
|
this.spring.register(Config.class).autowire();
|
||||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
||||||
this.springSecurityFilterChain,
|
this.springSecurityFilterChain,
|
||||||
(exchange,chain) ->
|
(exchange,chain) ->
|
||||||
|
@ -145,14 +151,10 @@ public class EnableWebFluxSecurityTests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public static class CustomPasswordEncoder {
|
|
||||||
@Autowired WebFilterChainProxy springSecurityFilterChain;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void passwordEncoderBeanIsUsed() {
|
public void passwordEncoderBeanIsUsed() {
|
||||||
|
this.spring.register(CustomPasswordEncoderConfig.class).autowire();
|
||||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
||||||
this.springSecurityFilterChain,
|
this.springSecurityFilterChain,
|
||||||
(exchange,chain) ->
|
(exchange,chain) ->
|
||||||
|
@ -174,7 +176,7 @@ public class EnableWebFluxSecurityTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
@EnableWebFluxSecurity
|
||||||
static class Config {
|
static class CustomPasswordEncoderConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public ReactiveUserDetailsService userDetailsRepository(PasswordEncoder encoder) {
|
public ReactiveUserDetailsService userDetailsRepository(PasswordEncoder encoder) {
|
||||||
return new MapReactiveUserDetailsService(User.withUsername("user")
|
return new MapReactiveUserDetailsService(User.withUsername("user")
|
||||||
|
@ -189,14 +191,10 @@ public class EnableWebFluxSecurityTests {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public static class FormLoginTests {
|
|
||||||
@Autowired WebFilterChainProxy springSecurityFilterChain;
|
|
||||||
@Test
|
@Test
|
||||||
public void formLoginWorks() {
|
public void formLoginWorks() {
|
||||||
|
this.spring.register(Config.class).autowire();
|
||||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
WebTestClient client = WebTestClientBuilder.bindToWebFilters(
|
||||||
this.springSecurityFilterChain,
|
this.springSecurityFilterChain,
|
||||||
(exchange,chain) ->
|
(exchange,chain) ->
|
||||||
|
@ -220,25 +218,9 @@ public class EnableWebFluxSecurityTests {
|
||||||
.expectHeader().valueMatches("Location", "/");
|
.expectHeader().valueMatches("Location", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
|
||||||
static class Config {
|
|
||||||
@Bean
|
|
||||||
public ReactiveUserDetailsService userDetailsRepository() {
|
|
||||||
return new MapReactiveUserDetailsService(User.withUsername("user")
|
|
||||||
.password("password")
|
|
||||||
.roles("USER")
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public static class MultiServerHttpSecurity {
|
|
||||||
@Autowired WebFilterChainProxy springSecurityFilterChain;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multiWorks() {
|
public void multiWorks() {
|
||||||
|
this.spring.register(MultiSecurityHttpConfig.class).autowire();
|
||||||
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build();
|
WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build();
|
||||||
|
|
||||||
client.get()
|
client.get()
|
||||||
|
@ -254,30 +236,21 @@ public class EnableWebFluxSecurityTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
@EnableWebFluxSecurity
|
||||||
static class Config {
|
static class MultiSecurityHttpConfig {
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
@Order(Ordered.HIGHEST_PRECEDENCE) @Bean public SecurityWebFilterChain apiHttpSecurity(
|
||||||
@Bean
|
ServerHttpSecurity http) {
|
||||||
public SecurityWebFilterChain apiHttpSecurity(ServerHttpSecurity http) {
|
http.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**"))
|
||||||
http
|
.authorizeExchange().anyExchange().denyAll();
|
||||||
.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**"))
|
|
||||||
.authorizeExchange()
|
|
||||||
.anyExchange().denyAll();
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean public SecurityWebFilterChain httpSecurity(ServerHttpSecurity http) {
|
||||||
public SecurityWebFilterChain httpSecurity(ServerHttpSecurity http) {
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean public ReactiveUserDetailsService userDetailsRepository() {
|
||||||
public ReactiveUserDetailsService userDetailsRepository() {
|
return new MapReactiveUserDetailsService(
|
||||||
return new MapReactiveUserDetailsService(User.withUsername("user")
|
User.withUsername("user").password("password").roles("USER").build());
|
||||||
.password("password")
|
|
||||||
.roles("USER")
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue