Spring Security 전체 필터의 흐름 (필터 체인의 순서가 고정되어 있음)
## 필터 체인 순서 요약
===요청/응답 전처리, URL/헤더/세션/CSRF===============
1. DisableEncodeUrlFilter
2. WebAsyncManagerIntegrationFilter
3. SecurityContextPersistenceFilter
4. HeaderWriterFilter
5. CorsFilter
6. CsrfFilter
7. LogoutFilter
===인증 처리 (폼, JWT, OAuth2 등)====================
8. [JwtAuthenticationFilter] ← (커스텀 필터) (UsernamePasswordAuthenticationFilter 전)
9. UsernamePasswordAuthenticationFilter
10. DefaultLoginPageGeneratingFilter
11. DefaultLogoutPageGeneratingFilter
12. BasicAuthenticationFilter
13. [BearerTokenAuthenticationFilter] ← (커스텀 필터) OAuth2 Resource Server 사용 시
===요청 캐싱, SecurityContext 확정, 익명 처리, 세션 관리, 예외 처리, 인가 최종 확인=======
14. RequestCacheAwareFilter
15. SecurityContextHolderAwareRequestFilter
16. AnonymousAuthenticationFilter
17. SessionManagementFilter
18. ExceptionTranslationFilter
19. FilterSecurityInterceptor (또는 AuthorizationFilter)
------------------------------------
→ DispatcherServlet → Controller
📌 각 필터가 하는 일 (일부만)
SecurityContextPersistenceFilter
- 요청마다 SecurityContext 생성/저장/초기화 담당
- 인증 정보(Authentication)을 request lifecycle에서 유지시킴
- 이거 없으면 SecurityContextHolder에 authentication이 저장되지 않아서 인증 자체가 불가능해짐.
UsernamePasswordAuthenticationFilter
- 스프링 기본 로그인(아이디/비밀번호) 처리
- JWT 쓰면 사실상 사용 안 하지만, 기본적으로 필터 체인엔 존재.
ExceptionTranslationFilter
- 인증/인가 예외 처리
- 403/401 예외를 적절하게 변환해주는 필터
FilterSecurityInterceptor
실제 인가(권한) 검증이 여기에 있음
- @PreAuthorize
- hasRole
- URL 인가 정책
모두 여기서 최종적으로 체크됨.
<필수 필터>
✔ 1. SecurityContextPersistenceFilter
✔ 2. ExceptionTranslationFilter
✔ 3. FilterSecurityInterceptor
나머지 필터는 조건부나 옵션
📌 커스텀 필터 끼워넣는 법 (addFilterBefore)
http.addFilterBefore(
new JwtAuthenticationFilter(jwtTokenProvider),
UsernamePasswordAuthenticationFilter.class
);
📌 JwtAuthenticationFilter 위치가 UsernamePasswordAuthenticationFilter의 앞인 이유
- JWT 인증은 요청마다 인증 정보를 세팅해야 하므로 항상 UsernamePasswordAuthenticationFilter보다 먼저 실행
- 폼 로그인은 실제 로그인 요청 처리용으로 뒤쪽에서 동작
- 이렇게 배치하면 이미 인증된 JWT 사용자는 로그인 없이 요청 처리가 가능하고, SecurityContext가 올바르게 세팅됨
- CORS, CSRF, ExceptionTranslation 등은 특정 순서를 강제함
CORS는 아주 앞에서 처리해야 함
CSRF는 인증 전에 검사해야 함
ExceptionTranslationFilter는 인증/인가 에러를 잡아야 하니 뒤에 있어야 함
참고 문헌
14. The Security Filter Chain
Spring Security’s web infrastructure is based entirely on standard servlet filters. It doesn’t use servlets or any other servlet-based frameworks (such as Spring MVC) internally, so it has no strong links to any particular web technology. It deals in H
docs.spring.io
spring-security/config/src/main/java/org/springframework/security/config/annotation/web/builders/FilterOrderRegistration.java at
Spring Security. Contribute to spring-projects/spring-security development by creating an account on GitHub.
github.com
'SPRING > Security' 카테고리의 다른 글
| JWT 토큰 생성 과정 - Claims에 관하여 (0) | 2025.11.19 |
|---|---|
| JWT 기본구조 알아보기 (0) | 2025.10.29 |
| 스프링 시큐리티 흐름 분석: DelegatingFilterProxy와 인증 체계 이해하기 (0) | 2025.10.28 |
| Spring Security 세션 Redis 저장 시 SerializationException 발생 원인과 해결 (0) | 2025.10.27 |
| 250919 (금) 스프링시큐리티 : JWT 로그인 / 정규표현식 (0) | 2025.09.19 |