Use the ServerProperties to add prefixes to paths
when server.servletPath is set we need to add prefixes to the security filter paths, and the /error path. Conflicts: spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementSecurityAutoConfiguration.java
This commit is contained in:
@@ -101,7 +101,7 @@ public class BatchAutoConfiguration {
|
||||
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
|
||||
factory.setDataSource(dataSource);
|
||||
factory.afterPropertiesSet();
|
||||
return (JobExplorer) factory.getObject();
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties.Headers;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -141,6 +142,9 @@ public class SpringBootWebSecurityConfiguration {
|
||||
@Autowired
|
||||
private SecurityProperties security;
|
||||
|
||||
@Autowired
|
||||
private ServerProperties server;
|
||||
|
||||
@Override
|
||||
public void configure(WebSecurity builder) throws Exception {
|
||||
}
|
||||
@@ -149,7 +153,8 @@ public class SpringBootWebSecurityConfiguration {
|
||||
public void init(WebSecurity builder) throws Exception {
|
||||
IgnoredRequestConfigurer ignoring = builder.ignoring();
|
||||
List<String> ignored = getIgnored(this.security);
|
||||
ignoring.antMatchers(ignored.toArray(new String[0]));
|
||||
String[] paths = this.server.getPathsArray(ignored);
|
||||
ignoring.antMatchers(paths);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@@ -265,4 +266,41 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
|
||||
|
||||
}
|
||||
|
||||
public String getServletPrefix() {
|
||||
String result = this.servletPath;
|
||||
if (result.contains("*")) {
|
||||
result = result.substring(0, result.indexOf("*"));
|
||||
}
|
||||
if (result.endsWith("/")) {
|
||||
result = result.substring(0, result.length() - 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] getPathsArray(Collection<String> paths) {
|
||||
String[] result = new String[paths.size()];
|
||||
int i = 0;
|
||||
for (String path : paths) {
|
||||
result[i++] = getPath(path);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] getPathsArray(String[] paths) {
|
||||
String[] result = new String[paths.length];
|
||||
int i = 0;
|
||||
for (String path : paths) {
|
||||
result[i++] = getPath(path);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getPath(String path) {
|
||||
String prefix = getServletPrefix();
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
return prefix + path;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
|
||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -55,6 +56,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(SecurityAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class));
|
||||
@@ -69,6 +71,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(SecurityAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "security.ignored:none");
|
||||
this.context.refresh();
|
||||
@@ -82,6 +85,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(SecurityAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false");
|
||||
this.context.refresh();
|
||||
@@ -94,6 +98,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(SecurityAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(AuthenticationManager.class));
|
||||
@@ -104,6 +109,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(TestConfiguration.class, SecurityAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertEquals(this.context.getBean(TestConfiguration.class).authenticationManager,
|
||||
@@ -117,7 +123,7 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context.register(EntityConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
|
||||
SecurityAutoConfiguration.class);
|
||||
SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class);
|
||||
// This can fail if security @Conditionals force early instantiation of the
|
||||
// HibernateJpaAutoConfiguration (e.g. the EntityManagerFactory is not found)
|
||||
this.context.refresh();
|
||||
|
||||
Reference in New Issue
Block a user