Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Security의 Filter Chain 동작 방식 #27

Open
suna-ji opened this issue Dec 22, 2024 · 2 comments
Open

Spring Security의 Filter Chain 동작 방식 #27

suna-ji opened this issue Dec 22, 2024 · 2 comments
Assignees
Labels
백엔드 질문 리스트의 직무 구분을 위한 라벨

Comments

@suna-ji
Copy link
Member

suna-ji commented Dec 22, 2024

📝 Spring Security의 Filter Chain 동작 방식

📚 주제:

Spring Security의 Filter Chain은 요청이 애플리케이션에 도달하기 전에 인증(Authentication) 및 권한 부여(Authorization) 과정을 처리하는 핵심 매커니즘입니다. 이 매커니즘은 요청의 보안을 책임지는 필터들의 체인으로 구성되어 있으며, 요청이 체인을 따라가며 각 필터에서 점진적으로 처리됩니다.
이번 스터디에서는 Filter Chain의 동작 방식과 주요 필터들의 역할에 대해 자세히 알아보겠습니다.👊

🎯 스터디 목표

  • Spring Security의 Filter Chain 구조를 이해한다.
  • 주요 필터의 역할을 이해한다.

📖 핵심 내용:

1. Filter Chain 구조와 FilterChainProxy의 역할

Spring Security의 Filter Chain은 여러 보안 필터로 구성된 체인입니다.
이 필터 체인을 관리하고 요청에 적합한 체인을 선택하여 실행하는 핵심 요소가 FilterChainProxy입니다.
DelegatingFilterProxy가 요청을 받으면 FilterChainProxy로 넘기고, 여기에서 URL 패턴에 따라 알맞은 SecurityFilterChain을 선택해서 실행합니다.

1-1. DelegatingFilterProxy

  • 일반적인 서블릿 필터로, Servlet 컨테이너에 등록되어 HTTP 요청을 처리합니다.
  • Spring Security와 같은 스프링 기반의 보안 필터를 Servlet 컨테이너와 연결하기 위해 사용됩니다.
  • Spring Context에 정의된 FilterChainProxy를 실제로 위임해서 요청을 처리
  • Spring Boot를 사용할 경우 자동으로 등록됩니다.

1-2. Springboot에서 DelegatingFilterProxy를 자동으로 등록하는 동작 방식 (심화)

스프링 시큐리티 의존성 추가하면 Spring Security의 자동 설정 클래스 SecurityAutoConfiguration이 활성화됩니다.
해당 클래스는 Spring Security 관련 Bean을 등록하며 여기에 DelegatingFilterProxyRegistrationBean도 포함됩니다.

1-3. FilterChainProxy 공식 문서 링크

  • Spring Security에서 모든 보안 관련 필터를 관리하는 진짜 핵심 요소

1-4. 단독 필터와 FilterChainProxy의 차이

아래 구조도에서 Filter0과 Filter2는 FilterChainProxy에 포함되지 않는걸 볼 수 있다.
FilterChainProxy는 Spring Security의 보안필터들을 관리하는 필터관리자라고 볼 수 있다. 즉 스프링 시큐리티에 의해 정의된 필터들만 관리한다.
즉, 구조도에 나온 Filter 0과 Filter 2는 스프링 시큐리티와 무관한 일반 Servlet Filter이다.
스프링 시큐리티 - 필터

2. 주요 Spring Security Filter

  • SecurityContextPersistenceFilter
    • 요청의 시작 시 SecurityContext를 초기화하고, 요청이 종료될 때 SecurityContext를 저장하거나 정리
    • 기존 세션에 인증 정보가 있다면 원복
  • UsernamePasswordAuthenticationFilter
    • 폼 기반 로그인 처리
    • 사용자 이름과 비밀번호를 받아서 인증 로직 수행
    • 인증 성공 시 AuthenticationManager를 통해 인증 정보를 SecurityContext에 저장
    • 우리는 JWT 토큰 발급 로직을 추가하도록 커스텀해서 사용하고 있음
  • BasicAuthenticationFilter
    • HTTP Basic 인증 요청 처리
    • 요청의 Authorization 헤더를 기반으로 인증 정보를 확인
  • ExceptionTranslationFilter
    • 인증 또는 권한 부여 과정에서 발생한 에러를 처리
    • 인증 실패 시 AuthenticationEntryPoint를 호출하고, 권한 부족 시 AccessDeniedHandler를 호출
  • FilterSecurityInterceptor
    • 최종 권한 검사 수행
    • 권한이 없을 경우 AccessDeniedException을 발생시킴
  • OAuth2LoginAuthenticationFilter
    • Oauth2 로그인을 처리

💡 참고 자료:
스프링 시큐리티 공식 문서

@suna-ji suna-ji added the 백엔드 질문 리스트의 직무 구분을 위한 라벨 label Dec 22, 2024
@suna-ji suna-ji self-assigned this Dec 22, 2024
@suna-ji suna-ji added this to the 8주차(12/23 월) milestone Dec 23, 2024
@jokbalkiller
Copy link
Member

Spring Filter가 주로 인증, 로그인, 권한 검사 등에 사용되군요 👍🏻

@yuiseo
Copy link
Member

yuiseo commented Jan 2, 2025

동시 로그인 제한 로직을 생성할 때 백엔드에서는 해당 filter chain을 사용해서 구현하신거로 들었었는데 이러한 로직으로 처리되는거였군요! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
백엔드 질문 리스트의 직무 구분을 위한 라벨
Projects
None yet
Development

No branches or pull requests

3 participants