I want to apply the default spring security auto-configuration based on a property from application.yml
. I thought I first exclude the auto-configuration class and then add it as an import to my configuration class that is conditional on that property:
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;
}
application.yml:
---
myapplication:
security: true
SecurityConfig.class:
@Configuration
@Import(SecurityAutoConfiguration.class)
@ConditionalOnProperty(prefix = "myapplication", name = "security", havingValue = "true")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
....
}
The SecurityAutoConfiguration
class never gets imported though:
Exclusions:
-----------
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
How do I achieve the desired outcome then?
UPDATE
Interestingly enough, if I do exactly the same with KafkaAutoConfiguration.class
it works as I expect - all Kafka beans get loaded and autoconfigured.
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
Add one of these to your classes:
This one if you want to make that class work if you
SET THE VALUE OF IT
andIT IS TRUE
.This one if you want to make that class work if you
DO NOT SET THE VALUE IN THE YAML FILE
orIT IS SET BUT THE THE VALUE IS FALSE
:So for example if you want to use the spring security for your project but sometimes you want to turn it off by setting the value of
myapplication.secured
then you will have a classes like:Your project will use this class in case you don't want security (myapplication.secured can be missing or set to false):
And your project will use this only if the
myapplication.enabled=true
is in your application.yaml file: