스프링 시큐리티

Ch02. 스프링 시큐리티 주요 아키텍처 이해 - 필터 초기화와 다중 보안 설정

webmaster 2022. 1. 16. 13:29
728x90

다중 설정 클래스

  • 설정 클래스 별로 보안 기능이 각각 작동
  •  
    설정클래스 별로 RequestMatcher 설정
    •  
      http.antMatcher(“/admin/**)
  • 설정클래스 별로 필터가 생성
    • 스프링 시큐리티가 초기화할 때, SecurityFilterChain을 FilterChainProxy에 저장한다.
  • FilterChainProxy 가 각 필터들 가지고 있음
    • SecurityFilterChins에서 List형식으로 가지고 있다
  • 요청에 따라 RequestMatcher와 매칭되는 필터가 작동하도록 함

동작 과정

  • RequestMacher에 따라 매칭 되는 SecurityFilterChain의 Filter를 실행하여 인증, 인가 처리를 한다.
  • RequestMacher에 따라 다중 설정 클래스를 설정할 수 있다는 의미이다.
@Order(0)
@Configuration
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/admin/**") //특정 URL이 있다
                .authorizeRequests()
                .anyRequest().authenticated() //인증을 받아야만 접근이 가능
            .and()
                .httpBasic();

   }
}
@Order(1)
@Configuration
class SecurityConfig2 extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().permitAll() //모든 접근 허용
                .and()
                .formLogin();
    }
}
  • 설정 파일을 초기화 할 때 순서를 다르게 초기화를 해야 하므로 @Order를 설정해야 한다.
  • Order 순서에 따라 스프링 시큐리티가 설정 파일을 읽어 들이기 때문에 Order가 바뀌게 되면 동작하지 않는다
    • Order가 더 낮은 설정을 먼저 체크하기 때문에 넓은 범위의 요청은 Order 값을 높게 설정해야 한다.
  • 설정 클래스를 초기화할 때 FilterChainProxy을 생성자로 설정 파일을 넘겨준다.
728x90