0

我想基于application.yml. 我以为我首先排除了自动配置类,然后将其作为导入添加到我的配置类中,该配置类以该属性为条件:

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

安全配置类:

@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按照我的预期做同样的事情——所有的 Kafka bean 都会被加载和自动配置。

   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


4

1 回答 1

0

将其中之一添加到您的课程中:

如果您想让该类工作,如果您SET THE VALUE OF ITIT IS TRUE.

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

如果您想让该课程正常工作,如果您DO NOT SET THE VALUE IN THE YAML FILEIT 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只有在 application.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();
    }
于 2020-12-13T18:28:17.323 回答