Disable EnableGlobalMethodSecurity annotation

后端 未结 3 664
耶瑟儿~
耶瑟儿~ 2020-12-06 20:58

Is there a way I can disable the global method security using the boolean securityEnabled from my config.properties? Any other approach?

@EnableWebSecurity          


        
3条回答
  •  温柔的废话
    2020-12-06 22:00

    I've managed this by defining a Spring "securityDisabled" profile and conditionally applying security config based off that. I'm using Spring Boot 2.0.2. I believe this should work if not using Spring Boot and in previous versions of Spring Boot, but I have not tested. It's possible some tweaks may be required to property and class names because I know in Spring 2.0 some of that changed.

    // In application.properties
    spring.profiles.include=securityDisabled
    

    Then my security config looks like this:

    @Configuration
    public class SecurityConfig {
    
      // When the securityDisabled profile is applied the following configuration gets used
      @Profile("securityDisabled")
      @EnableWebSecurity
      public class SecurityDisabledConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // Configure http as needed from Spring Security defaults when
            // NO security is desired
        }
      }
    
      // When the securityDisabled profile is NOT applied the following configuration gets used
      @Profile("!securityDisabled")
      @EnableGlobalMethodSecurity(prePostEnabled = true)
      @EnableWebSecurity
      public class SecurityEnabledConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // Configure http as needed from Spring Security defaults when
            // security is desired
        }
      }
    }
    

提交回复
热议问题