[ODOP] 82일차 - 필터 초기화와 다중 보안 설정

[ODOP] 82일차 - 필터 초기화와 다중 보안 설정

다중 설정 클래스

  • 설정 클래스 별로 보안 기능이 각각 작동

  • 설정 클래스 별로 RequestMatcher 설정

    http.antMatcher("/admin/**")...
    
  • 설정 클래스 별로 필터가 생성

  • FilterChainProxy 가 각 필터들을 가지고 있음

  • 요청에 따라 RequestMatcher 와 매칭되는 필터가 작동하도록 함

    위 그림 같은 경우, /admin 으로 시작하는 URL 이 들어오면 왼쪽의 Filter 가 사용되고

    /item 이런식으로 아무거나 들어오게 되면 오른쪽의 Filter 가 동작하게 된다.

    이 기준은 Order 로 지정되며 다음과 같이 설정할 수 있다

    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.annotation.Order;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    
    @Order(1)
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig1 extends WebSecurityConfigurerAdapter { 
    		@Override
        protected void configure(HttpSecurity http) throws Exception {
    				http.antMatcher("/admin/pay")
    						.authorizeRequests()
    						.anyRequest().authenticated()
    						.httpBasic()
    				;
    		}
    }
    
    @Order(2)
    @Configuration
    public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
    		@Override
        protected void configure(HttpSecurity http) throws Exception {
    				http.antMatcher("/admin/**")
    						.authorizeRequests()
    						.anyRequest().authenticated()
    						.loginForm();
    				;
    		}
    }
    

    이미지로 보면 다음과 같다

    -----2023-09-01----3.54.49