복붙노트

[SPRING] Spring 보안에서 registerGlobal (), configure (), configureGlobal (), configureGlobalSecurity 간의 차이점

SPRING

Spring 보안에서 registerGlobal (), configure (), configureGlobal (), configureGlobalSecurity 간의 차이점

필자는 메모리 내 인증 생성과 같은 일을하는 세 가지 코드 스 니펫 아래에 있습니다. 그러면 다른 메소드 이름으로 정의하는 데 어떻게 영향을 줍니까?

첫 번째:

public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER").and()
        .withUser("admin").password("password").roles("USER","ADMIN");
    }
}

두번째 것:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
 }

세번째 것:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
}

네번째:

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)     throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}

업데이트 1 : 내가 추가하고 싶은 한가지 더 :

configure () 메서드는 WebSecurityConfigurerAdapter 클래스에 있지만 다른 메서드는 존재하지 않습니다.

업데이트 2 :

샘플 프로젝트의 메서드 이름을 아래로 변경했습니다. 놀랍게도 사용자를 인증하고 작업하고 있습니다.

너는 그것의 이름을 짓고 작동한다.

@Autowired
public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");      
}

해결법

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

    1.사실 두 가지 옵션 만 있습니다.

    사실 두 가지 옵션 만 있습니다.

    옵션 1 : 주석 만 사용하십시오 (예제 1, 3 및 4 - 샘플에 관련 주석을 포함하지 않았 음을 참고하십시오)

    registerGlobal, configureGlobal, configureGlobalSecurity는 똑같은 일을합니다. 취향에 따라 방법의 이름을 지정할 수 있습니다. 유일한 제약 조건은 다음과 같습니다.

    (메서드의 이름이 중요하지 않음을 알 수 있듯이, 코드 샘플로 인터넷 검색을 할 때 매우 다양한 메서드 이름을 찾은 것입니다.)

    다음은 어떻게 보이는지 보여주는 예입니다.

    @EnableWebSecurity
    public class MyConfiguration {
    
        @Autowired
        public void whatever(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    
        ...
    
    }
    

    옵션 2 : 주석 + 메소드 오버라이드 사용 (예제 2를 포함)

    configure 재정의는 WebSecurityConfigurerAdapter (또는 WebSecurityConfigurer를 구현하는 @Configuration 클래스)의 하위 클래스에서 편리한 방법이지만 다른 옵션과 동일한 효과가 있습니다.

    두 가지 접근 방식이 동일한 효과를 가지므로 맛 / 프로그래밍 스타일에 관한 질문 일뿐입니다.

    첫 번째 옵션은 구성을 단일 클래스로 유지할 필요가 있거나 필요할 때 필요하지만 @Configuration 클래스는 이미 다른 클래스를 확장합니다 (전체 WebSecurityConfigurer 인터페이스를 구현하지 않으려는 경우).

    마지막으로 자세히 설명해 드리겠습니다. Spring은 Spring 구성의 개발 속도를 높이기 위해 확장 할 수있는 많은 Adapter 클래스를 제공합니다.

    예를 들어, 일반적으로 사용되는 Adapter : WebMvcConfigurerAdapter를 예로 들어 보겠습니다. 다음과 같은 아주 간단한 구성으로 시작합니다.

    @EnableWebMvc
    @Configuration
    @ComponentScan({ "com.company.mypackage" })
    public class SpringWebConfig extends WebMvcConfigurerAdapter {
    
    }
    

    여기서 중요한 것은 클래스가 이미 Adapter 클래스를 확장했기 때문에 다른 클래스를 확장 할 수 없습니다.

    이제 보안 구성을 추가해야합니다. 기존 SpringWebConfig 구성 클래스에 포함 시키거나 새로운 보안 특정 구성 클래스를 생성 할 수 있습니다. 다음은 두 가지 접근법의 샘플입니다.

    1) 단일 @Configuration 클래스 접근법

    여기서 중요한 점은 SpringWebConfig가 WebMvcConfigurerAdapter + @EnableWebSecurity를 ​​확장 한 것입니다.

    @EnableWebMvc
    @Configuration
    @ComponentScan({ "com.company.mypackage" })
    @EnableWebSecurity
    public class SpringWebConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        public void whatever(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }     
    }
    

    2) 특정 보안 @Configuration 클래스

    여기서 주목해야 할 중요한 점은 MySecurityConfig가 WebSecurityConfigurerAdapter를 확장 한 것입니다.

    SpringWebConfig를 그대로 유지하고 새 @Configuration 클래스를 만듭니다.

    @Configuration
    @EnableWebSecurity
    public class MySecurityConfig extends WebSecurityConfigurerAdapter {
        @Overide
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    }
    
  2. ==============================

    2.registerGlobal (AuthenticationManagerBuilder auth)과 configureGlobal (AuthenticationManagerBuilder auth)의 차이점은 다음과 같습니다.

    registerGlobal (AuthenticationManagerBuilder auth)과 configureGlobal (AuthenticationManagerBuilder auth)의 차이점은 다음과 같습니다.

    출처: "Hello Spring Security Java Config"가이드에서 "Spring 보안 구성 만들기"장.

    protected void configure (AuthenticationManagerBuilder auth)는 WebSecurityConfigurer (및 해당 인터페이스 WebSecurityConfigurer)에서 제공하는 메서드입니다.이 메서드는 저장 방법이 더 많지만 그 결과는 다릅니다.

  3. from https://stackoverflow.com/questions/35218354/difference-between-registerglobal-configure-configureglobal-configureglo by cc-by-sa and MIT license