|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2019 the original author or authors.
|
|
|
+ * Copyright 2002-2021 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.
|
|
@@ -41,6 +41,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
+import org.springframework.security.config.core.GrantedAuthorityDefaults;
|
|
|
import org.springframework.security.config.test.SpringTestContext;
|
|
|
import org.springframework.security.config.test.SpringTestContextExtension;
|
|
|
import org.springframework.security.core.Authentication;
|
|
@@ -75,6 +76,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|
|
*
|
|
|
* @author Rob Winch
|
|
|
* @author Eleftheria Stein
|
|
|
+ * @author Yanming Zhou
|
|
|
*/
|
|
|
@ExtendWith(SpringTestContextExtension.class)
|
|
|
public class ExpressionUrlAuthorizationConfigurerTests {
|
|
@@ -232,6 +234,28 @@ public class ExpressionUrlAuthorizationConfigurerTests {
|
|
|
this.mvc.perform(requestWithAdmin).andExpect(status().isForbidden());
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void getWhenHasAnyRoleUserWithTestRolePrefixConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception {
|
|
|
+ this.spring.register(RoleUserWithTestRolePrefixConfig.class, BasicController.class).autowire();
|
|
|
+ // @formatter:off
|
|
|
+ MockHttpServletRequestBuilder requestWithUser = get("/")
|
|
|
+ .with(user("user")
|
|
|
+ .authorities(new SimpleGrantedAuthority("TEST_USER")));
|
|
|
+ // @formatter:on
|
|
|
+ this.mvc.perform(requestWithUser).andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHasAnyRoleUserWithEmptyRolePrefixConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception {
|
|
|
+ this.spring.register(RoleUserWithEmptyRolePrefixConfig.class, BasicController.class).autowire();
|
|
|
+ // @formatter:off
|
|
|
+ MockHttpServletRequestBuilder requestWithUser = get("/")
|
|
|
+ .with(user("user")
|
|
|
+ .authorities(new SimpleGrantedAuthority("USER")));
|
|
|
+ // @formatter:on
|
|
|
+ this.mvc.perform(requestWithUser).andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void getWhenRoleUserOrAdminConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception {
|
|
|
this.spring.register(RoleUserOrAdminConfig.class, BasicController.class).autowire();
|
|
@@ -263,6 +287,28 @@ public class ExpressionUrlAuthorizationConfigurerTests {
|
|
|
this.mvc.perform(requestWithRoleOther).andExpect(status().isForbidden());
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void getWhenRoleUserOrAdminWithTestRolePrefixConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception {
|
|
|
+ this.spring.register(RoleUserOrAdminWithTestRolePrefixConfig.class, BasicController.class).autowire();
|
|
|
+ // @formatter:off
|
|
|
+ MockHttpServletRequestBuilder requestWithUser = get("/")
|
|
|
+ .with(user("user")
|
|
|
+ .authorities(new SimpleGrantedAuthority("TEST_USER")));
|
|
|
+ // @formatter:on
|
|
|
+ this.mvc.perform(requestWithUser).andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenRoleUserOrAdminWithEmptyRolePrefixConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception {
|
|
|
+ this.spring.register(RoleUserOrAdminWithEmptyRolePrefixConfig.class, BasicController.class).autowire();
|
|
|
+ // @formatter:off
|
|
|
+ MockHttpServletRequestBuilder requestWithUser = get("/")
|
|
|
+ .with(user("user")
|
|
|
+ .authorities(new SimpleGrantedAuthority("USER")));
|
|
|
+ // @formatter:on
|
|
|
+ this.mvc.perform(requestWithUser).andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void getWhenHasIpAddressConfiguredAndIpAddressMatchesThenRespondsWithOk() throws Exception {
|
|
|
this.spring.register(HasIpAddressConfig.class, BasicController.class).autowire();
|
|
@@ -628,6 +674,44 @@ public class ExpressionUrlAuthorizationConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class RoleUserWithTestRolePrefixConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .authorizeRequests()
|
|
|
+ .anyRequest().hasAnyRole("USER");
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ GrantedAuthorityDefaults grantedAuthorityDefaults() {
|
|
|
+ return new GrantedAuthorityDefaults("TEST_");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class RoleUserWithEmptyRolePrefixConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .authorizeRequests()
|
|
|
+ .anyRequest().hasAnyRole("USER");
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ GrantedAuthorityDefaults grantedAuthorityDefaults() {
|
|
|
+ return new GrantedAuthorityDefaults("");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
@EnableWebSecurity
|
|
|
static class RoleUserOrAdminConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -642,6 +726,44 @@ public class ExpressionUrlAuthorizationConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class RoleUserOrAdminWithTestRolePrefixConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .authorizeRequests()
|
|
|
+ .anyRequest().hasAnyRole("USER", "ADMIN");
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ GrantedAuthorityDefaults grantedAuthorityDefaults() {
|
|
|
+ return new GrantedAuthorityDefaults("TEST_");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class RoleUserOrAdminWithEmptyRolePrefixConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .authorizeRequests()
|
|
|
+ .anyRequest().hasAnyRole("USER", "ADMIN");
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ GrantedAuthorityDefaults grantedAuthorityDefaults() {
|
|
|
+ return new GrantedAuthorityDefaults("");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
@EnableWebSecurity
|
|
|
static class HasIpAddressConfig extends WebSecurityConfigurerAdapter {
|
|
|
|