- Spring Security 초기화 - HttpSecurity, SecurityConfigurer, SecurityFilterChain2025년 01월 28일
- starryeye
- 작성자
- 2025.01.28.오후06:39
Spring Security 는 요청에 대한 인증, 인가 처리를 Servlet Filter 를 통해 수행한다.
해당 Servlet Filter 를 만드는 초기화 과정 및 아키텍처를 알아보겠다.
SecurityBuilder
SecurityBuilder 는 ...
빌더 클래스로 Security 처리와 설정을 담당하는 빈 객체들을 생성하는 역할을 담당한다.
대표적으로 WebSecurity 와 HttpSecurity 가 있다.
HttpSecurity
위는 HttpSecurityConfiguration 클래스에 존재하는 HttpSecurity 빈 등록 코드이다.
HttpSecurity 객체는 spring boot auto configuration 에 의해 자동으로 등록되는 빈인 것을 알 수 있다.
scope 은 싱글톤이 아닌 prototype 으로 빈 주입 때 마다. 서로다른 HttpSecurity 객체가 주입된다.
위는 HttpSecurity 의 formLogin 메서드이다.
HttpSecurity 는 위와 같이 다양한 인증 기술을 설정할 수 있도록 돕는다. (+인가)
설정은 위 메서드 리턴 타입을 보면 알 수 있다. (메서드 체인 방식)
this.getOrApply 메서드를 통해 HttpSecurity 의 configurers 필드에 해당 설정을 add 한다.
따라서, HttpSecurity 로 다양한 인증, 인가 기술을 원하는데로 설정할 수 있다.
formLogin 메서드에서 FormLoginConfigurer 객체를 파라미터로 넣어줘야 하는 것을 볼 수 있는데
FormLoginConfigurer 는 SecurityConfigurer 의 구현체이다.
위는 HttpSecurity 의 build 메서드를 호출하면 수행되는 메서드들 중 하나이다.
HttpSecurity 를 build 하기 전에 HttpSecurity 에 formLogin 과 같은 다양한 설정을 하게 되면
configurers 에 다양한 설정들이 적재가 되고
해당 설정들(SecurityConfigurer 구현체)을 init, configure 메서드를 호출해줌으로 하여
초기화 과정을 수행하는 것을 알 수 있다.
해당 메서드의 리턴 타입은 제네릭 타입이지만, 코드를 보다보면 결국 아래에서 단서를 알 수 있다.
DefaultSecurityFilterChain 으로 결국 SecurityFilterChain 을 생성하게 된다.
SecurityConfigurer 에서 init, configure 을 호출하면,
최종 생성할 SecurityfilterChain 내부에 해당 설정에 맞는 여러 Servlet Filter 들을 생성하게되며
다양한 Servlet Filter 들을 생성하며 filter 내부에도 그에 역할에 필요한 여러 객체들을 함께 생성한다.
결국..
HttpSecurity(SecurityBuilder) 로 빌드 하면 SecurityFilterChain 이 생성되는데
SecurityFilterChain 내부에는 개발자가 여러 SecurityConfigurer 로 설정한,
다양한 Security 기술에 필요한 Servlet Filter 들이 생성된다.
'Spring > Security' 카테고리의 다른 글
Spring Security 초기화 - DelegatingFilterProxy, FilterChainProxy, SecurityFilterChain, WebSecurity (0) 2025.01.28 Spring Security 초기화 - 의존성에 의한 자동 구성 (0) 2025.01.28 Spring Security 소개 (0) 2025.01.28 다음글이전글이전 글이 없습니다.댓글