mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Change @Conditional for existing security configuration
Fixed by using `WebSecurityConfiguration` to detect existing security configuration (not the annotation). Fixes gh-151
This commit is contained in:
parent
023faf092f
commit
2543ef7072
@ -54,6 +54,10 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implemen
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
public HibernateJpaAutoConfiguration() {
|
||||
System.err.println("Init");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
|
@ -43,6 +43,7 @@ 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.IgnoredRequestConfigurer;
|
||||
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.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
@ -80,7 +81,7 @@ import org.springframework.security.web.util.matcher.AnyRequestMatcher;
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConditionalOnClass({ EnableWebSecurity.class })
|
||||
@ConditionalOnMissingBean(annotation = EnableWebSecurity.class)
|
||||
@ConditionalOnMissingBean(WebSecurityConfiguration.class)
|
||||
public class SecurityAutoConfiguration {
|
||||
|
||||
private static List<String> DEFAULT_IGNORED = Arrays.asList("/css/**", "/js/**",
|
||||
|
@ -19,12 +19,19 @@ package org.springframework.boot.autoconfigure.security;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.TestUtils;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer;
|
||||
import org.springframework.boot.autoconfigure.ComponentScanDetector;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
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.context.initializer.LoggingApplicationContextInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@ -93,6 +100,20 @@ public class SecurityAutoConfigurationTests {
|
||||
this.context.getBean(AuthenticationManager.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJpaCoexistsHappily() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(EntityConfiguration.class, TestConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
|
||||
SecurityAutoConfiguration.class);
|
||||
// This can fail if security @Conditionals force early instantiation of the
|
||||
// HibernateJpaAutoConfiguration
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(JpaTransactionManager.class));
|
||||
}
|
||||
|
||||
private static AnnotationConfigWebApplicationContext debugRefresh(
|
||||
AnnotationConfigWebApplicationContext context) {
|
||||
TestUtils.addEnviroment(context, "debug:true");
|
||||
@ -105,6 +126,13 @@ public class SecurityAutoConfigurationTests {
|
||||
return context;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackageClasses = { City.class })
|
||||
@Import(ComponentScanDetector.class)
|
||||
protected static class EntityConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user