BackEnd/Spring
[Spring] Spring Security 설정하기(FilterChain 처리 과정)
ryuneng
2025. 1. 18. 00:00
반응형
📍 Security Filter Chain
- Spring Security에서 제공하는 인증, 인가를 위한 필터들을 관리하는 객체
- 기본적으로 제공하는 필터들이 있으며, 개발자는 애플리케이션의 개발 목적에 맞게 사용자 정의 필터를 추가하거나 SecurityFilterChain의 필터를 설정할 수 있다.
주요 필터
- UsernamePasswordAuthenticationFilter
- username/password로 로그인하려고 하는지 체크해서 사용자 인증을 수행하고,
인증이 완료되면 사용자 인증 작업을 수행하는 필터
- username/password로 로그인하려고 하는지 체크해서 사용자 인증을 수행하고,
- AnonymousAuthenticationFilter
- 앞선 여러 필터를 통해서 인증이 아직 완료되지 않았다면 Authentication객체를 생성하고,
익명사용자로 설정하는 역할을 수행한다.
- 앞선 여러 필터를 통해서 인증이 아직 완료되지 않았다면 Authentication객체를 생성하고,
- LogoutFilter
- 클라이언트의 요청이 로그아웃 요청인지 파악하고, 로그아웃 작업을 수행한다.
- SecurityContextPersistenceFilter
- 클라이언트의 요청이 접수되면 SecurityContextRepository에서 SecurityContext를 가져오거나 생성하는 역할을 수행한다.
* SecurityContext는 인증된 객체(Authentication)이 저장되는 객체다.
- 클라이언트의 요청이 접수되면 SecurityContextRepository에서 SecurityContext를 가져오거나 생성하는 역할을 수행한다.
📌 FilterChain 구조

1. AuthenticationFilter
- 인증을 담당하는 필터
- 일반적으로 아이디/비밀번호를 이용해서 사용자 인증을 하기 때문에
AuthenticationFilter의 구현체 중에서 UsernamePasswordAuthenticationFilter가 처리한다.
2. Authentication
- 현재 접근하는 주체(사용자)의 정보와 권한을 표현하는 인터페이스
- Authentication은 아래의 정보를 표현한다.
- Principal(접근주체) - 보호받는 리소스에 접근하려는 사용자
- Credential(비밀번호) - 접근주체의 비밀번호
- Authority(권한) - 접근주체가 보유한 권한
- UsernamePasswordAuthenticationToken은 Authentication 인터페이스의 구현체다.
- 아이디/비밀번호를 이용하는 사용자 인증에서 사용되는 Authentication객체다.
- 아이디/비밀번호를 이용하는 로그인 요청을 보내면
UsernamePasswordAuthenticationFilter가 실행되고,
이 필터가 UsernamePasswordAuthenticationToken을 생성한다. 이 객체에는 사용자가 로그인폼에서 입력한 아이디가 Principal에, 비밀번호가 Credential에 저장되어 있다.
3. AuthenticationManager
- 인증에 대한 부분은 AuthenticationManager를 통해서 처리한다.
- ProviderManager는 AuthenticationManager 인터페이스의 구현체다.
- 실질적인 인증처리는 AuthenticationManager에 등록된 AuthenticationProvider를 통해서 처리된다.
4. AuthenticationProvider
- 인증 전 Authentication객체를 전달받아서 인증작업을 수행하고,
인증 후 Authentication 객체를 반환한다. - 인증 작업에 필요한 사용자 정보를 UserDetailsService를 이용해서 데이터베이스에 조회한다.
- 조회한 사용자정보의 비밀번호와 인증 전 Authentication에 저장된 비밀번호를 비교해서 인증작업을 수행한다.
5. UserDetails
- 사용자 정보를 표현하는 인터페이스
- UserDetails 인터페이스를 구현한 사용자 정의 UserDetails 클래스를 구현해야 한다.
- UserDetails 구현클래스에는 사용자이름, 비밀번호, 보유권한, 계정잠금 여부, 계정활성화 여부, 계정만료 여부, 계정비밀번호 만료여부와 같은 정보를 제공한다.
6. UserDetailsService
- 아이디를 전달받아서 사용자정보를 표현하는 UserDetails 객체를 반환하는 추상메서드가 정의된 인터페이스
- 추상메서드 UserDetails loadUserByUsername(String username)
- 사용자정의 UserDetailsService 구현클래스를 작성해야 한다.
* UserDetails, UserDetailsService를 개발자가 직접 구현해야 하는 이유
: 개발환경에 따라 구현 방법이 천차만별이기 때문에
✔️ FilterChain 처리 과정

- 사용자는 아이디/비밀번호를 입력하고 로그인 요청을 서버로 보낸다.
- AuthenticaitonFilter는 로그인 요청을 받는다.
AuthenticationFilter의 구현체인 UsernamePasswordAuthenticationFilter는
인증 전 Authentication 구현객체(UsernamePasswordAuthenticationToken)를 생성한다.
인증 전 Authentication 객체에는 아래의 값이 저장된다.principal = 사용자 아이디, credential = 비밀번호, authenticated = false, authorities = null
- AuthenticationFilter는 인증 전 Authentication 객체를 AuthenticationManager에게 전달해서 인증을 요청한다.
- AuthenticationManager는 구현체인 ProviderManager에 등록된 AuthenticationProvider에게 인증 전 Authentication 객체를 전달해서 인증처리를 위임한다.
- AuthenticationProvider는 UserDetailsService의 loadUserByUsername(String username) 메서드를 실행해서 사용자 정보를 조회한다.
- 조회된 사용자정보는 UserDetails 객체를 생성해서 저장한다.
- AuthenticationProvider는 조회된 사용자정보가 담긴 UserDetails객체를 전달받고, 사용자 인증을 수행한다.
- 인증 전 Authentication객체의 credential에 저장된 비밀번호와 조회된 사용자 정보가 표현된 UserDetails객체의 저장된 비밀번호를 비교한다.
- AuthenticationProvider는 인증이 완료되면 인증 후 Authentication 객체를 생성해서 AuthenticationManager에게 반환한다.
principal = UserDetails객체, credential = 비밀번호, authenticated = true, authorities = List<GrantedAuthority> 객체
- AuthenticationManager는 인증 후 Authentication객체를 반환한다.
< 해당 글은 velog에서 이전하며 옮겨온 글로, 가독성이 좋지 않을 수 있는 점 양해 부탁드립니다. >
🔗 velog 버전 보기 : https://velog.io/@ryuneng2/Spring-Security-FilterChain