다중 보안 설정, 필터 초기화 방식
-
다중 보안 설정은 즉, WebSecurityConfigurerAdapter 상속클래스가 다중임을 뜻함.
-
각각의 WebSecurityConfugurerAdapter 객체들은 각각의 HttpSecurity를 통해 보안 API 에 맞는 Filter들을 가지고 있음
-
각각의 SpringSecurity 설정 클래스마다 SecurityFilterChain 객체를 각각 생성.
-
WebSecurityConfigurerAdapter 마다 지정된 API 에 맞는 Filter들의 명단을 SecurityFilterChain 객체의 Filters 필드에 저장
-
또한 각각의 RequestMapping 들을 SecurityFilterChain 객체의 RequestMatcher 필드(AntRequestMatcher)에 저장
-
FilterChainProxy 는 각각의 SecurityFilterChain 들을 SecurityFilterChains 필드에 저장
-
FilterChainProxy는 필터 목록을 2개 가지게 됨
-
사용자가 특정 Url로 Request
-
각각의 SecurityFilterChain 객체 중 Request Url 과 매칭되는 Filter 목록을 가져와 인증, 인가처리를 진행
큰틀에서 보자
-
사용자가 특정 Url 로 GET 요청
-
FilterChainProxy 가 요청을 수렴
-
요청을 처리할 Filter 목록를 선택할 때 SecurityFilterChain 객체의 RequestMatcher에 RequestUrl 과 matches 한다.
-
요청 Url 과 맞는 SecurityFilterChain 객체가 있다면 해당 SecurityFilterChain 객체의 Filter 명단으로 인증, 인가 처리를 진행
즉, RequestMatcher 만 잘 잡혀 있다면, FilterChainProxy 는 해당 SecurityFilterChain 객체의 Filter 명단을 사용한다.
다중 보안 설정 클래스
**단순히 SpringSecurity 설정클래스를 2개 주고 서버를 구동하면 순서를 지정하라(@Order) 는 에러메시지가 송출되니 순번을 정해놓아야한다.
@Configuration
@EnableWebSecurity
@Order(1)
public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/admin/**")//특정 Url의
.authorizeRequests()//인가 요청에 대한
.anyRequest() //어떠한 요청이라도
.authenticated()//인증완료시 접근 가능하다
.and()//다른기능을 추가
.httpBasic();//httpBasic 방식으로 안중 적업을 한다.
}
}
@Configuration
@Order(0)
class SecurityConfig2 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()//인가 요청에 대한
.anyRequest()//어떠한 요청이라도
.permitAll()//접근을 허가한다.
.and()//다른기능을 추가
.formLogin();// 폼 로그인 방식을 쓰겠다.
}
}
주의 사항
요청시 FilterChainProxy의 SecurityFilterChain 객체의 RequestMatcher 체크 순서도 @Order 로 순번이 나뉜다.
-
url 범위가 넒은 쪽의 순번이 뒤로 가야한다.
-
url 범위가 좁은 쪽은 순번이 앞으로 가야한다.
'springframework > 시작하자SpringSecurity' 카테고리의 다른 글
17.SecurityContextHolder, SecurityContext (0) | 2020.09.22 |
---|---|
16.Authentication (0) | 2020.09.21 |
14.DelegatingFilterProxy, FilterChainProxy (0) | 2020.09.21 |
13. 사이트 간 요청위조 : CSRF, CsrfFilter (0) | 2020.09.21 |
12.ExceptionTranslationFilter, RequestCacheAwareFilter (0) | 2020.09.21 |