Principal 이 Secured Resource 에 접근할 때 Authentication 을 통해 신원을 확인하고 Authorization 을 통해 접근 권한을 검증하는 보안 프레임워크
컨테이너에 무관하게 적용한다. WAR, EAR 형태로 배포될 수 있다. Servlet Application에서는 Filter 기반으로 동작한다. Spring 기반 애플리케이션이 아니어도 Spring Security 적용이 가능하다. @Transactional 처럼 보안적인 이슈와 business logic 을 분리한다. basic, form, cookies 등 다양한 인증 방법을 사용 가능하다. 또 properties file, 메모리, RDBMS, LDAP 등 사용자 정보 저장 방법도 다양하다.

Authentication = 인증 = 로그인 확인 = 너 누구야?
Authorization = 인가 = 권한 확인 = 너 이거 해도 돼?
principal = 사용자 식별 정보, 보통 username
credential = 증명 정보, 보통 password
1. SpringFilterChain
filter 는 Servlet 요청을 처리하기 전에 동작한다. Spring MVC 는 Filter 를 거친 후 DispatcherServlet 에서 시작한다.

DelegatingFilterProxy는 Servlet Container의 필터 체인과 Spring Security의 springSecurityFilterChain Bean을 이어주는 어댑터/대리자다. Filter 는 원래 Servlet Container 가 관리하는 객체이다. Spring Bean 으로만 등록하면 Servlet Container 의 Filter Chain 에 자동으로 들어가지 않는다. 그래서 Servlet Container 입장에서는 필터처럼 보이는 객체가 필요하고, Spring 입장에서는 Spring Bean에게 위임해주는 객체가 필요하다.
Spring Security 는 하나의 필터가 모든 일을 하는 게 아니라, 여러 SecurityFilter 가 순서대로 지나가면서 보안 처리를 나눠서 한다. 최초 적용 chain 에서 경로 기반으로 순서대로 filter 를 확인한다. 요청 -> SecurityContext 준비 -> CORS/CSRF 처리 -> 로그인/인증 처리 -> 예외 처리 준비 -> 권한 검사 -> Controller
2. Authentication
AbstractAuthenticationProcessingFilter 는 사용자 요청을 가로채서 Authentication 객체 (대표적 구현체 = UsernamePasswordAuthenticationToken(username, password)) 를 생성한다. Authentication은 AuthenticationManager 에게 전달되어서 인증을 처리한다.
Principal, Credentials, Authorities(권한) 으로 구성된다.
Principal: 사용자를 식별하는 정보 (username / UserDetails 타입 객체)
Credentials: 자격증 명으로 Password (또는 토큰, 인증서 등)
Authorities: ROLE, ADMIN, ROLE, USER 등 사용자가 부여 받은 권한
인증 전 사용자가 인증을 위해 AuthenticationManager 에게 제공한다.
SecurityContext 라는 현재 사용자에 대한 Authentication 을 세션에 보관하는 객체로 SecurityContextHolder 를 통해 접근한다. ThreadLocal 에 저장되어 동일 스레드 내에서 언제든 현재 사용자 정보에 접근이 가능하다.
AuthenticationManager = 인증을 맡는 대표 인터페이스
ProviderManager = AuthenticationManager의 대표 구현체
AuthenticationProvider = 실제 인증을 처리하는 담당자들
1. UsernamePasswordAuthenticationFilter가 요청을 가로챔
2. UsernamePasswordAuthenticationToken 생성
3. AuthenticationManager에게 인증 요청
4. 실제 구현체 ProviderManager가 요청을 받음
5. ProviderManager가 DaoAuthenticationProvider에게 위임
6. DaoAuthenticationProvider가 UserDetailsService로 user01 조회
7. PasswordEncoder로 비밀번호 확인
8. 성공하면 인증 완료 Authentication 반환
9. SecurityContext에 저장
'Spring' 카테고리의 다른 글
| [Spring] 클라이언트로 정보 전달하기 (0) | 2024.08.04 |
|---|---|
| 스프링 생초보! 이미지 업로드 게시판 구현을 위한 빌드업 (2) | 2024.07.21 |
| [인프런 Inflearn - 김영한] 스프링 입문 (프로젝트 환경설정) (1) | 2024.07.11 |