我想基于application.yml
中的属性应用默认的spring安全性自动配置。我想我首先排除了自动配置类,然后将其作为导入添加到我的配置类中,该配置类以该属性为条件:
MyApplication.class:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
ApplicationProperties.class
@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapplication")
public class ApplicationProperties {
private boolean security;
}
应用程序.yml:
---
myapplication:
security: true
SecurityConfig.class:
@Configuration
@Import(SecurityAutoConfiguration.class)
@ConditionalOnProperty(prefix = "myapplication", name = "security", havingValue = "true")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
....
}
SecurityAutoConfiguration
类从未被导入:
Exclusions:
-----------
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
那么我该如何达到预期的结果呢?
更新
有趣的是,如果我对KafkaAutoConfiguration.class
做同样的操作,它会像我预期的那样工作——所有的Kafkabean都会被加载并自动配置。
KafkaAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.kafka.core.KafkaTemplate' (OnClassCondition)
KafkaAutoConfiguration#kafkaAdmin matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaAdmin; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaConsumerFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaProducerFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.ProducerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaProducerListener matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.support.ProducerListener; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaTemplate matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaTemplate; SearchStrategy: all) did not find any beans (OnBeanCondition)
Exclusions:
-----------
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
将其中一个添加到类中:
如果你想让这个类在SET THE VALUE OF IT
和IT IS TRUE
的情况下工作,这一个。
@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "true", matchIfMissing = false)
如果你想让这个类工作,如果你DO NOT SET THE VALUE IN THE YAML FILE
或IT IS SET BUT THE THE VALUE IS FALSE
:
@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "false", matchIfMissing = true)
例如,如果你想为你的项目使用spring安全性,但有时你想通过设置myapplication.secured
的值来关闭它,那么你会有一个类,比如:
如果您不需要安全性,您的项目将使用此类(myapplication.secured可能丢失或设置为false(:
@Configuration
@EnableWebSecurity
@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "false", matchIfMissing = true)
public class NoSpringSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
只有当myapplication.enabled=true
在您的应用程序中时,您的项目才会使用它。yaml文件:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "true", matchIfMissing = false)
@RequiredArgsConstructor
public class SpringSecuritz extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/**")
.authenticated();
}