복붙노트

[SPRING] EnableGlobalMethodSecurity 주석 사용 중지

SPRING

EnableGlobalMethodSecurity 주석 사용 중지

내 config.properties에서 부울 securityEnabled를 사용하여 전역 메소드 보안을 비활성화 할 수있는 방법이 있습니까? 다른 방법은 없나요?

@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled=true) 
@PropertySource("classpath:config.properties")  
public class SecurityConfig 
  extends WebSecurityConfigurerAdapter {    

  @Value("${securityconfig.enabled}") 
  private boolean securityEnabled;

  ...

}

해결법

  1. ==============================

    1.이렇게하는 가장 쉬운 방법은 다음과 같습니다.

    이렇게하는 가장 쉬운 방법은 다음과 같습니다.

    예 :

    @EnableWebSecurity
    @Configuration
    @PropertySource("classpath:config.properties")
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        ...
    }
    
    @EnableGlobalMethodSecurity
    @Configuration
    public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    
        @Value("${securityconfig.enabled}")
        private boolean securityEnabled;
    
        protected MethodSecurityMetadataSource customMethodSecurityMetadataSource() {
            return securityEnabled ? new SecuredAnnotationSecurityMetadataSource() : null;
        }    
    }
    
  2. ==============================

    2.나는 스프링 "securityDisabled"프로파일을 정의하고 그것을 기반으로 보안 설정을 조건부로 적용함으로써 이것을 관리했다. 스프링 부트 2.0.2를 사용하고 있습니다. 나는 Spring Boot를 사용하지 않고 Spring Boot의 이전 버전에서 이것이 작동해야한다고 생각하지만 테스트를 거치지 않았다. Spring 2.0에서 일부 변경된 사항을 알고 있기 때문에 속성 및 클래스 이름에 약간의 개조가 필요할 수도 있습니다.

    나는 스프링 "securityDisabled"프로파일을 정의하고 그것을 기반으로 보안 설정을 조건부로 적용함으로써 이것을 관리했다. 스프링 부트 2.0.2를 사용하고 있습니다. 나는 Spring Boot를 사용하지 않고 Spring Boot의 이전 버전에서 이것이 작동해야한다고 생각하지만 테스트를 거치지 않았다. Spring 2.0에서 일부 변경된 사항을 알고 있기 때문에 속성 및 클래스 이름에 약간의 개조가 필요할 수도 있습니다.

    // In application.properties (could also based in via command line or environment variable)
    spring.profiles.active=securityDisabled
    // Or even with
    spring.profiles.include=securityDisabled
    

    그런 다음 내 보안 설정은 다음과 같습니다.

    @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
        }
      }
    }
    
  3. from https://stackoverflow.com/questions/22176236/disable-enableglobalmethodsecurity-annotation by cc-by-sa and MIT license