개요

SecurityContextHolder.png

시큐리티 구조.png

1. 전체 흐름

[1. 클라이언트 로그인 요청]
	    - 사용자가 username/password 전송
    │
    │
    ▼
[2. 인증 시도용 Authentication 객체 생성]
	    - UsernamePasswordAuthenticationToken(username, password)
	    - 인증 전 상태: isAuthenticated = false
    │
    │
    ▼
[3. AuthenticationManager.authenticate() 호출]
	    - AuthenticationManager(ProviderManager)가 등록된 AuthenticationProvider 목록 순회
	    - 각 AuthenticationProvider에 대해 supports(Authentication) 체크
         ├─ supports() = true → AuthenticationManager가 해당 AuthenticationProvider에 인          │   증 위임(delegate)
         └─ supports() = false → 다음 AuthenticationProvider 체크
	    - 모든 AuthenticationProvider가 false → AuthenticationException 발생
    │
    │
    │
    ▼
[4. AuthenticationProvider 내부 인증 수행]
	    - DB 조회 또는 외부 인증 (예: OAuth2, JWT)
	    - 비밀번호 검증 (passwordEncoder.matches)
	    - 계정 상태 확인 (INACTIVE, DELETED 등)
    │
    ├─ 성공 → 인증된 Authentication 반환
	  │   new UsernamePasswordAuthenticationToken(
		│       PrincipalDetails,   // UserDetails 구현체
		│       null,               // credentials 제거
		│       authorities
		│   )
		│   ┌──────────────────────────────────┐
		│   │ Authentication (인증 완료 상태)  │
		│   │──────────────────────────────────│
		│   │ principal    = UserDetails       │
		│   │ credentials  = null              │
		│   │ authorities  = ROLE_xxx          │
		│   │ authenticated = true             │
		│   └──────────────────────────────────┘
    │
    │
    └─ 실패 → AuthenticationException 던짐
          (BadCredentialsException, DisabledException 등)
	  │          
    │
    ▼
[5. SecurityContextHolder 저장]
    - 인증된 Authentication 객체를 SecurityContext에 저장
    - 이후 요청에서 인증 정보 접근 가능

2. Authentication 인터페이스

AuthenticationSpring Security에서 인증 객체를 추상화한 인터페이스를 말한다.

public interface Authentication extends Principal, Serializable {
    Collection<? extends GrantedAuthority> getAuthorities();
    Object getCredentials();
    Object getDetails();
    Object getPrincipal();
    boolean isAuthenticated();
    void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

즉, 인증 주체와 인증 상태, 권한 정보를 담는 역할을 합니다.


3. UsernamePasswordAuthenticationToken