- Published on
- •👁️
Spring Security 탐구 - 동작 원리
- Authors

- Name
- River
Spring Security 탐구 - 동작 원리
1. 참고 자료
- 개발자 유미 (Youtube)
- 스프링 시큐리티 구조 : 스프링 공식 문서
- Spring Security 의존성 내부 실제 클래스
- Chat GPT, Claude AI
- 내가 Spring Security를 적용하고 커스텀한 프로젝트
2. Security 의존성이 없는 상태
클라이언트 요청 ⇒ Controller

- 클라이언트 요청은 서버 컴퓨터의 WAS(톰캣)의 필터들을 통과한 뒤 스프링 컨테이너의 컨트롤러에 도달한다.
- 즉, 클라이언트 요청이 서버에 도착하게 되면 특정 URL을 매핑하여 페이지나 데이터를 응답해준다.

3. Security 의존성을 추가한 상태
- Spring Security가 WAS의 필터단으로부터 클라이언트 요청을 가로챈 후 FilterChainProxy에게 요청을 전달한다.
- WAS의 필터단에 하나의 필터를 만들어서 넣어서 해당 필터에서 요청을 가로챈다.
- 이렇게 가로챈 요청은 Spring Container 내부에 구현되어 있는 Spring Security 감시 로직을 거치게 된다.
- Spring Security 감시 로직을 마친 후 다시 WAS의 다음 필터로 복귀한다

상세 과정

- Security 의존성을 추가하면 WAS의 필터 체인 에 DelegatingFilterProxy라는 필터가 추가된다.
- SecurityAutoConfiguration 클래스에 의해 DelegatingFilterProxy 객체가 생성되고 WAS의 필터 체인에 등록된다.
- DelegatingFilterProxy 객체가 생성될 때 FilterChainProxy의 Bean 이름을 전달해 Bean을 가져올 수 있게 한다.
DelegatingFilterProxy 필터가 요청을 가로채서 Spring Container 내부에 구현된 Spring Security 필터 체인을 거치게 한다.
이러한 Spring Security 필터 체인을 다 마치면 다시 WAS의 필터 체인으로 돌아간 뒤 Spring Container로 들어간다.
4. Spring의 SecurityFilterChain
- Spring Security의 로직은 여러 개의 필터들이 나열된 필터 체인 형태로 구성되어 있다.
- 각각의 필터에서 CSRF, 로그아웃, 로그인, 인가 등 여러 작업을 수행
- SecurityFilterChain
- 일련의 과정들을 수행하는 필터들의 묶음
- 여러 개의 SecurityFilterChain- SecurityFilterChain은 하나만 둘 수도 있지만 여러 개를 사용할 수도 있다.

- httpSecurity.securityMatcher(url)
@Bean SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception { http .securityMatcher("/api/**", "/oauth2/**", "/login/oauth2/**", "/login/**") .authorizeHttpRequests(
- 해당 SecurityFilterChain은 이 경로에 유효하다고 정하는 것
- 다만 이렇게 경로를 나누어 지정하거나 순서를 지정해줘야 오류가 발생하지 않는다.
🔆 용어 정리
- DelegatingFilterProxy- FilterChainProxy Spring Bean을 찾아 요청을 넘겨주는 서블릿 필터
- FilterChainProxy- Spring Security 의존성을 추가하면 DelegatingFilterProxy에 의해 호출되는 클래스 - SecurityFilterChain들을 들고 있는 Bean이다. - DelegatingFilterProxy가 요청을 가로채서 전달할 목적지
- SecurityFilterChain- 스프링 시큐리티 필터들의 묶음으로 실제 시큐리티 로직이 처리되는 부분- FilterChainProxy가 SecurityFilterChain을 들고 있다.