Polish ServerHttpSecurity testing

This commit is contained in:
Rob Winch 2017-10-12 15:03:50 -05:00
parent 015cc2203e
commit 30487c3b4b
5 changed files with 78 additions and 33 deletions

View File

@ -0,0 +1,43 @@
/*
* Copyright 2002-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.security.config.annotation.web.reactive;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
/**
* @author Rob Winch
* @since 5.0
*/
public class ServerHttpSecurityConfigurationBuilder {
public static final UserDetails USER = User.withUsername("user").password("password").roles("USER").build();
public static final UserDetails ADMIN = User.withUsername("admin").password("password").roles("USER","ADMIN").build();
public static ServerHttpSecurity http() {
return new ServerHttpSecurityConfiguration().httpSecurity();
}
public static ServerHttpSecurity httpWithDefaultAuthentication() {
ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(new MapReactiveUserDetailsService(USER,ADMIN));
return http()
.authenticationManager(authenticationManager);
}
}

View File

@ -18,6 +18,7 @@ package org.springframework.security.config.web.server;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder; import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@ -26,13 +27,14 @@ import org.springframework.test.web.reactive.server.WebTestClient;
* @since 5.0 * @since 5.0
*/ */
public class AuthorizeExchangeBuilderTests { public class AuthorizeExchangeBuilderTests {
ServerHttpSecurity http = ServerHttpSecurity.http(); ServerHttpSecurity http = ServerHttpSecurityConfigurationBuilder.httpWithDefaultAuthentication();
ServerHttpSecurity.AuthorizeExchangeBuilder authorization = this.http.authorizeExchange();
@Test @Test
public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() { public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() {
this.authorization.pathMatchers(HttpMethod.POST, "/a", "/b").denyAll(); this.http
this.authorization.anyExchange().permitAll(); .authorizeExchange()
.pathMatchers(HttpMethod.POST, "/a", "/b").denyAll()
.anyExchange().permitAll();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -60,8 +62,10 @@ public class AuthorizeExchangeBuilderTests {
@Test @Test
public void antMatchersWhenPatternsThenAnyMethod() { public void antMatchersWhenPatternsThenAnyMethod() {
this.authorization.pathMatchers("/a", "/b").denyAll(); this.http
this.authorization.anyExchange().permitAll(); .authorizeExchange()
.pathMatchers("/a", "/b").denyAll()
.anyExchange().permitAll();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -88,19 +92,26 @@ public class AuthorizeExchangeBuilderTests {
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() { public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() {
this.authorization.pathMatchers("/incomplete"); this.http
this.authorization.pathMatchers("/throws-exception"); .authorizeExchange()
.pathMatchers("/incomplete");
this.http
.authorizeExchange()
.pathMatchers("/throws-exception");
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void anyExchangeWhenFollowedByMatcherThenThrowsException() { public void anyExchangeWhenFollowedByMatcherThenThrowsException() {
this.authorization.anyExchange().denyAll(); this.http
this.authorization.pathMatchers("/never-reached"); .authorizeExchange().anyExchange().denyAll()
.pathMatchers("/never-reached");
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void buildWhenMatcherDefinedWithNoAccessThenThrowsException() { public void buildWhenMatcherDefinedWithNoAccessThenThrowsException() {
this.authorization.pathMatchers("/incomplete"); this.http
.authorizeExchange()
.pathMatchers("/incomplete");
this.http.build(); this.http.build();
} }

View File

@ -23,6 +23,7 @@ import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.PageFactory;
import org.springframework.security.authentication.ReactiveAuthenticationManager; import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager; import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
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.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -42,20 +43,15 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 5.0 * @since 5.0
*/ */
public class FormLoginTests { public class FormLoginTests {
private UserDetails user = User.withUsername("user").password("password").roles("USER").build(); private ServerHttpSecurity http = ServerHttpSecurityConfigurationBuilder.httpWithDefaultAuthentication();
private ServerHttpSecurity http = ServerHttpSecurity.http();
ReactiveAuthenticationManager manager = new UserDetailsRepositoryReactiveAuthenticationManager(new MapReactiveUserDetailsService(this.user));
@Test @Test
public void defaultLoginPage() { public void defaultLoginPage() {
SecurityWebFilterChain securityWebFilter = this.http SecurityWebFilterChain securityWebFilter = this.http
.authenticationManager(this.manager)
.authorizeExchange() .authorizeExchange()
.anyExchange().authenticated() .anyExchange().authenticated()
.and() .and()
.formLogin().and() .formLogin().and()
.logout().and()
.build(); .build();
WebTestClient webTestClient = WebTestClientBuilder WebTestClient webTestClient = WebTestClientBuilder
@ -92,7 +88,6 @@ public class FormLoginTests {
@Test @Test
public void customLoginPage() { public void customLoginPage() {
SecurityWebFilterChain securityWebFilter = this.http SecurityWebFilterChain securityWebFilter = this.http
.authenticationManager(this.manager)
.authorizeExchange() .authorizeExchange()
.pathMatchers("/login").permitAll() .pathMatchers("/login").permitAll()
.anyExchange().authenticated() .anyExchange().authenticated()

View File

@ -20,6 +20,7 @@ import org.junit.Test;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.springframework.security.authentication.ReactiveAuthenticationManager; import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager; import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
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.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -34,20 +35,14 @@ import org.springframework.security.test.web.reactive.server.WebTestClientBuilde
*/ */
public class LogoutBuilderTests { public class LogoutBuilderTests {
private UserDetails user = User.withUsername("user").password("password").roles("USER").build(); private ServerHttpSecurity http = ServerHttpSecurityConfigurationBuilder.httpWithDefaultAuthentication();
private ServerHttpSecurity http = ServerHttpSecurity.http();
ReactiveAuthenticationManager manager = new UserDetailsRepositoryReactiveAuthenticationManager(new MapReactiveUserDetailsService(this.user));
@Test @Test
public void defaultLogout() { public void defaultLogout() {
SecurityWebFilterChain securityWebFilter = this.http SecurityWebFilterChain securityWebFilter = this.http
.authenticationManager(this.manager)
.authorizeExchange() .authorizeExchange()
.anyExchange().authenticated() .anyExchange().authenticated()
.and() .and()
.formLogin().and()
.logout().and()
.build(); .build();
WebTestClient webTestClient = WebTestClientBuilder WebTestClient webTestClient = WebTestClientBuilder
@ -84,11 +79,9 @@ public class LogoutBuilderTests {
@Test @Test
public void customLogout() { public void customLogout() {
SecurityWebFilterChain securityWebFilter = this.http SecurityWebFilterChain securityWebFilter = this.http
.authenticationManager(this.manager)
.authorizeExchange() .authorizeExchange()
.anyExchange().authenticated() .anyExchange().authenticated()
.and() .and()
.formLogin().and()
.logout() .logout()
.logoutUrl("/custom-logout") .logoutUrl("/custom-logout")
.and() .and()

View File

@ -24,6 +24,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.authentication.ReactiveAuthenticationManager; import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder; import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
import org.springframework.security.web.server.WebFilterChainProxy; import org.springframework.security.web.server.WebFilterChainProxy;
import org.springframework.security.web.server.context.ServerSecurityContextRepository; import org.springframework.security.web.server.context.ServerSecurityContextRepository;
@ -45,15 +46,17 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ServerHttpSecurityTests { public class ServerHttpSecurityTests {
@Mock ServerSecurityContextRepository contextRepository;
@Mock @Mock
ReactiveAuthenticationManager authenticationManager; private ServerSecurityContextRepository contextRepository;
@Mock
private ReactiveAuthenticationManager authenticationManager;
ServerHttpSecurity http; private ServerHttpSecurity http;
@Before @Before
public void setup() { public void setup() {
this.http = ServerHttpSecurity.http().headers().and(); this.http = ServerHttpSecurityConfigurationBuilder.http()
.authenticationManager(this.authenticationManager);
} }
@Test @Test