Spring Boot default auto-configuration based on property

1.2k views Asked by At

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


1

There are 1 answers

1
stackstack293 On

Add one of these to your classes:

This one if you want to make that class work if you SET THE VALUE OF IT and IT IS TRUE.

@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "true", matchIfMissing = false)

This one if you want to make that class work if you DO NOT SET THE VALUE IN THE YAML FILE or IT IS SET BUT THE THE VALUE IS FALSE:

@ConditionalOnProperty(prefix = "myapplication", value = "secured", havingValue = "false", matchIfMissing = true)

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):

@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();
    }

}

And your project will use this only if the myapplication.enabled=true is in your application.yaml file:

@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();
    }