Don’t call ignoring.antMatchers([]) as empty array now maps to /**

Previously, if security.ignored was set to none and the error controller
was disabled, there would be no paths to ignore and we would call
IgnoredRequestConfigurer.antMatchers with an empty array. While a bit
pointless, this had no effect on Spring Security’s configuration.

This behaviour has changed in the latest 4.0.3 snapshots [1]. An empty
array passed to IgnoredRequestConfigurer.antMatchers now maps to /**. As
Spring Boot configures its ignored paths with highest precedence this
means that security is now disabled for every path.

This commit updates both the management security and application
security configuration to avoid calling antMatchers with an empty
array, thereby ensuring that we don’t inadvertently ignore every path.
Even if the change to Spring Security is reverted we can keep this
change. The behaviour will remain the same and, arguably, it makes the
intent of our configuration clearer.

Closes gh-4345

[1] 8663ac4173
pull/4334/merge
Andy Wilkinson 9 years ago
parent 1e4257daed
commit 02d7e2826c

@ -69,6 +69,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.AnyRequestMatcher; import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher; import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -163,7 +164,9 @@ public class ManagementWebSecurityAutoConfiguration {
} }
if (this.server != null) { if (this.server != null) {
String[] paths = this.server.getPathsArray(ignored); String[] paths = this.server.getPathsArray(ignored);
ignoring.antMatchers(paths); if (!ObjectUtils.isEmpty(paths)) {
ignoring.antMatchers(paths);
}
} }
} }

@ -144,7 +144,7 @@ public class ManagementWebSecurityAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none"); EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none");
this.context.refresh(); this.context.refresh();
// Just the application and management endpoints now // Just the application and management endpoints now
assertEquals(3, assertEquals(2,
this.context.getBean(FilterChainProxy.class).getFilterChains().size()); this.context.getBean(FilterChainProxy.class).getFilterChains().size());
} }

@ -39,7 +39,6 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@ -49,6 +48,7 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationEn
import org.springframework.security.web.header.writers.HstsHeaderWriter; import org.springframework.security.web.header.writers.HstsHeaderWriter;
import org.springframework.security.web.util.matcher.AnyRequestMatcher; import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -147,13 +147,14 @@ public class SpringBootWebSecurityConfiguration {
@Override @Override
public void init(WebSecurity builder) throws Exception { public void init(WebSecurity builder) throws Exception {
IgnoredRequestConfigurer ignoring = builder.ignoring();
List<String> ignored = getIgnored(this.security); List<String> ignored = getIgnored(this.security);
if (this.errorController != null) { if (this.errorController != null) {
ignored.add(normalizePath(this.errorController.getErrorPath())); ignored.add(normalizePath(this.errorController.getErrorPath()));
} }
String[] paths = this.server.getPathsArray(ignored); String[] paths = this.server.getPathsArray(ignored);
ignoring.antMatchers(paths); if (!ObjectUtils.isEmpty(paths)) {
builder.ignoring().antMatchers(paths);
}
} }
private String normalizePath(String errorPath) { private String normalizePath(String errorPath) {

@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.security; package org.springframework.boot.autoconfigure.security;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
@ -56,7 +54,6 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.web.FilterChainProxy; import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -91,10 +88,9 @@ public class SecurityAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class)); assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class));
// 5 for static resources and one for the rest // 4 for static resources and one for the rest
List<SecurityFilterChain> filterChains = this.context assertEquals(5,
.getBean(FilterChainProxy.class).getFilterChains(); this.context.getBean(FilterChainProxy.class).getFilterChains().size());
assertEquals(5, filterChains.size());
} }
@Test @Test
@ -165,7 +161,7 @@ public class SecurityAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none"); EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none");
this.context.refresh(); this.context.refresh();
// Just the application endpoints now // Just the application endpoints now
assertEquals(2, assertEquals(1,
this.context.getBean(FilterChainProxy.class).getFilterChains().size()); this.context.getBean(FilterChainProxy.class).getFilterChains().size());
} }

Loading…
Cancel
Save