Spring Security 기본 구조
인증(Authentication)과 인가(Authorization)
인증(Authentication)과 인가(Authorization)는 보안을 구성하는 영역들이다.
인증은 해당 사용자가 본인이 맞는지를 확인하는 절차이다. 인증된 사용자는 권한을 부여 받게 된다.
인가는 인증된 사용자가 요청한 자원에 대한 접근 권한을 갖고 있는지 확인하는 절차이다.
Principal은 자원에 접근하는 대상을 말하며, Credential은 Principal의 비밀번호이다.
Spring Security
스프링 시큐리티(Spring Security)는 스프링에 기반한 보안 프레임워크이다.
스프링 시큐리티는 스프링에 대한 의존성을 없애기 위해 필터를 기반으로 동작한다. 즉 Spring MVC, 비즈니스 코드와 분리하여 동작하고 관리할 수 있다.
DelegationFilterProxy와 FilterChainPorxy
스프링은 스프링 컨테이너와 연결할 수 있는 DelegationFilterProxy라는 필터를 제공한다.
스프링 빈으로 구현한 필터들을 DelegationFilterProxy를 통해 등록하고, 해당 필터들을 서블릿 표준 필터처럼 동작하게 한다.
스프링 시큐리티는 FilterChainPorxy를 통해 다양한 필터 기능을 스프링 빈으로 구현하고, DelegationFilterProxy를 통해 필터에 추가한다.
FilterChainProxy는 스프링 시큐리티에서 제공하는 특수 필터로 다양한 인스턴스를 SecurityFilterChain에 위임하고 있다.
일종의 스프링 시큐리티를 위한 디스패쳐 서블릿이라고 볼 수 있다.
SecurityFilterChain
SecurityFilterChain은 스프링 시큐리티에서 제공하는 기본적인 보안 관련 필터들이다.
1. SecurityContextPersistenceFilter: SecurityContextRepository에서 SecurityContext를 가져오거나 저장한다.
2. LogoutFilter: 설정된 로그아웃 URL에 대한 요청을 감시하며, 해당 사용자를 로그아웃 처리한다.
3. (UsernamePassword)AuthenticationFilter: (form 기반) 로그인 URL에 대한 요청을 감시하며, 사용자 인증을 처리한다.
- AuthenticationManager을 통해 인증을 처리한다.
- 인증 성공 시, 얻은 Authentication 객체를 SecurityContext에 저장 -> AuthenticationSuccessHandler를 실행한다.
- 인증 실패 시, AuthenticationFailureHandler를 실행한다.
4. DefultLoginPageGeneratingFilter: 로그인 form URL에 대한 요청을 감시한다.
5. BasicAuthenticationFilter: HTTP 기본 인증 헤더를 감시한다.
6. RequestCacheAwareFitler: 로그인 성공 후, 기존 요청 정보를 재구성하기 위해 사용한다.
7. SecurityContextHolderAwareRequestFilter
- HttpServletRequestWrapper를 상속한 SecurityContextHolderAawreRequestWrapper로 HttpServletRequest 정보를 감싼다.
- SecurityContextHolderAwareRequestWrapper는 다음 필터에게 정보를 제공한.
8. AnonymousAuthenticationFilter: 해당 필터가 호출되는 시점까지 사용자 정보가 인증되지 않았다면, 인증 토큰에 사용자가 익명 사용자로 나타난다.
9. SessionManagerFilter: 이 필터는 인증된 사용자와 관련된 모든 세션을 추적한다.
10. ExceptionTranslationFilter: 보호된 요청을 처리할 때 발생할 수 있는 예외를 위임, 전달한다.
11. FilterSecurityInterceptor: AccessDecisionManager로써 권한 부여처리를 위임하여 접근 제어를 쉽게 해준다.