728x90
- Spring Security 는 Spring MVC Controller에서 Callable을 실행하는 비동기 스레드에 SecurityContext를 자동으로 설정하도록 지원
- Spring Security는 WebAsyncManager와 통합하여 SecurityContextHolder에서 사용 가능한 SecurityContext를 Callable에서 접근 가능하도록 해줌
→ 기본적으로 부모 ThreadLocal과 자식 ThreadLocal은 공유가 되지 않는데, WebAsyncManager를 사용하여 부모 ↔ 자식 간의 SecurityContext를 공유할 수 있도록 함
WebAsyncManagerIntegrationFilter
- SecurityContext와 WebAsyncManager 사이의 통합을 제공하며 WebAsyncManager를 생성하고 SecurityContextCallableProcessingInterceptor를 WebAsyncManager에 등록
WebAsyncManager
- 스레드 풀의 비동기 스레드를 생성하고 Callable 를 받아 실행시키는 주체로서 등록된 SecurityContextCallableProcessingInterceptor를 통해 현재 스레드(부모 스레드)가 보유하고 있는 SecurityContext 객체를 비동기 스레드의 ThreadLocal에 저장
코드 구현
- Main Thread → 부모 스레드 / 비동기 Thread → 자식 스레드
- 비동기 스레드가 수행하는 Callable 영역 내에서 자신의 ThreadLocal 에 저장된 SecurityContext 를 참조 가능
→ 이는 부모 스레드가 가지고 있는 SecurityContext와 동일한 객체 - @Async나 다른 비동기 기술은 스프링 시큐리티와 통합되어 있지 않기 때문에 비동기 스레드에 SecurityContext가 공유되지 않음
→ 공유하기 위해서는 설정 파일에서 아래의 코드 추가 필요SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLE THREADLOCAL)
흐름도
- 클라이언트 요청
- 부모 스레드가 수신하여 WebAsyncManagerIntegrationFilter가 수신하여 아래 두 개를 생성
- WebAsyncManager
- SecurityContextCallableProcessingInterceptory
- WebAsyncManager가 비동기 실행할 수 있는 쓰레드(ThreadPoolExecutor) 생성
- 메인(부모) 스레드의 SecurityContext를 SeucrityContextCallableProcessingInterceptor에 저장
- 비동기 스레드 생성 후 ThreadLocal이 빈 상태인데, 메인(부모) 스레드의 SecurirtyContext를 가지고 와서 저장
- Callable에서 자신의 ThreadLocal에 저장된 Security Context를 참조
728x90
'Language > Spring Security' 카테고리의 다른 글
Custom DSLs - HttpSecurity.with(AbstractHttpConfigurer) (0) | 2024.08.03 |
---|---|
다중 보안 설정 (0) | 2024.08.03 |
Spring MVC 통합 - @AuthenticationPrincipal (0) | 2024.08.03 |
Servlet API 통합 - SecurityContextHoladerAwareRequestFilter (0) | 2024.08.03 |
인가 이벤트 - Authorization Events (0) | 2024.08.03 |