Contents
1️⃣ 전체 요청 흐름2️⃣ SecurityFilterChain이란?3️⃣ 주요 필터 종류와 역할① SecurityContextHolderFilter② CsrfFilter③ UsernamePasswordAuthenticationFilter④ BasicAuthenticationFilter⑤ BearerTokenAuthenticationFilter (JWT 사용 시)⑥ AnonymousAuthenticationFilter⑦ ExceptionTranslationFilter⑧ AuthorizationFilter4️⃣ 필터 실행 순서 (중요)5️⃣ 인증과 인가의 차이6️⃣ JWT + 세션 혼합 구조일 경우7️⃣ 커스텀 필터 추가 방법8️⃣ 응답 시에도 필터를 거칠까?9️⃣ 필터 vs 인터셉터 차이🔥 중요한 필터한 줄 정리Spring Security는 단순한 라이브러리가 아니다.
🔐 요청을 가로채서 인증(Authentication)과 인가(Authorization)를 처리하는 보안 프레임워크
그 핵심이 바로 Security Filter Chain이다.
1️⃣ 전체 요청 흐름
브라우저에서 요청이 들어오면 실제 내부 흐름은 다음과 같다.
클라이언트 ↓ 서블릿 컨테이너 (Tomcat) ↓ FilterChainProxy (Spring Security) ↓ SecurityFilter Chain (여러 보안 필터들) ↓ DispatcherServlet ↓ Controller
👉 즉, Controller에 도달하기 전에 보안 검사가 끝난다.
2️⃣ SecurityFilterChain이란?
Spring Security는 하나의 필터가 아니라
여러 개의 보안 필터가 순서대로 실행되는 구조
이 구조를 FilterChainProxy가 관리한다.
3️⃣ 주요 필터 종류와 역할
Spring Security에는 10개 이상의 필터가 존재한다.
실무에서 중요한 것 위주로 정리해보자.
① SecurityContextHolderFilter
역할
- SecurityContext 초기화
- ThreadLocal에 사용자 정보 저장 준비
의미
“이 요청에서 사용할 인증 정보 공간을 만든다”
② CsrfFilter
역할
- CSRF 공격 방지
- POST, PUT, DELETE 요청 시 토큰 검증
동작
- 요청에 CSRF 토큰 존재 확인
- 세션에 저장된 토큰과 비교
- 불일치 시 403 반환
③ UsernamePasswordAuthenticationFilter
역할
- 로그인 요청 처리
- ID / PW 인증 수행
동작
- /login 요청 감지
- username, password 추출
- AuthenticationManager로 전달
- 성공 시 SecurityContext에 저장
④ BasicAuthenticationFilter
역할
- HTTP Basic 인증 처리
동작
Authorization 헤더:
Authorization: Basic base64(username:password)을 해석하여 인증 수행
⑤ BearerTokenAuthenticationFilter (JWT 사용 시)
역할
- Authorization: Bearer 토큰 처리
- JWT 인증
동작
- 헤더에서 토큰 추출
- JWT 검증
- 인증 객체 생성
- SecurityContext에 저장
⑥ AnonymousAuthenticationFilter
역할
- 인증이 없는 사용자를 “익명 사용자”로 설정
즉,
authentication ==null상태가 되지 않도록 기본 Authentication을 만들어준다.
⑦ ExceptionTranslationFilter
역할
- 인증/인가 예외 처리
처리 대상
- AuthenticationException → 401
- AccessDeniedException → 403
⑧ AuthorizationFilter
역할
- URL 접근 권한 검사
예:
.requestMatchers("/admin/**").hasRole("ADMIN")권한 없으면 403 반환
4️⃣ 필터 실행 순서 (중요)
Spring Security는 순서가 생명이다.
대표적인 흐름:
SecurityContextHolderFilter ↓ CsrfFilter ↓ UsernamePasswordAuthenticationFilter ↓ BearerTokenAuthenticationFilter ↓ AnonymousAuthenticationFilter ↓ ExceptionTranslationFilter ↓ AuthorizationFilter
👉 인증(Authentication) 먼저
👉 인가(Authorization) 나중
5️⃣ 인증과 인가의 차이
구분 | 의미 |
인증 | 누구인지 확인 |
인가 | 접근 권한이 있는지 확인 |
예:
- 로그인 성공 → 인증 완료
- /admin 접근 → 인가 검사
6️⃣ JWT + 세션 혼합 구조일 경우
요청이 들어오면:
- 세션 인증 확인
- 없으면 JWT 필터 실행
- 둘 다 없으면 익명 사용자 처리
이 구조를 직접 커스터마이징 가능하다.
7️⃣ 커스텀 필터 추가 방법
http.addFilterBefore(newJwtFilter(), UsernamePasswordAuthenticationFilter.class);
의미:
UsernamePasswordAuthenticationFilter 전에 JwtFilter 실행
순서 지정이 매우 중요하다.
8️⃣ 응답 시에도 필터를 거칠까?
✔ 요청은 필터 체인을 통과한다
✔ 응답도 역방향으로 필터를 통과한다
특히:
- 예외 처리
- SecurityContext 정리
- 세션 저장
이 과정이 응답 단계에서 수행된다.
9️⃣ 필터 vs 인터셉터 차이
구분 | 필터 | 인터셉터 |
위치 | 서블릿 레벨 | Spring MVC 레벨 |
보안 | 주로 사용 | 거의 사용 안 함 |
실행 시점 | DispatcherServlet 이전 | Controller 전/후 |
👉 보안은 필터 레벨에서 처리해야 안전하다.
🔥 중요한 필터
- UsernamePasswordAuthenticationFilter
- BearerTokenAuthenticationFilter
- ExceptionTranslationFilter
- AuthorizationFilter
이 4개를 이해하면 80% 이해한 것이다.
한 줄 정리
Spring Security Filter Chain은 Controller에 도달하기 전에 인증과 인가를 처리하는 보안 전용 필터 집합이다.
Share article