Spring Security

1. Filter Chain 기반 구조

HTTP 요청 -> Security Filter Chain -> Controller

핵심 Filter:

  • UsernamePasswordAuthenticationFilter: 로그인 처리
  • JwtAuthenticationFilter: JWT 토큰 검증
  • AuthorizationFilter: 권한 검사

2. 인증(Authentication) 과정

 1. 사용자가 로그인 시도
 2. AuthenticationManager가 처리
 3. UserDetailsService에서 사용자 정보 조회
 4. PasswordEncoder로 비밀번호 검증
 5. 성공 시 SecurityContext에 인증 정보 저장

3. 인가(Authorization) 과정

@PreAuthorize("hasRole('ADMIN')")  // 메서드 실행 전 권한 체크
public void adminOnly() { }

// 또는 URL 기반
http.authorizeRequests()
    .antMatchers("/admin/**").hasRole("ADMIN")

4. SecurityContext

  • 현재 사용자의 인증 정보 저장
  • ThreadLocal 기반으로 스레드별 관리
  • SecurityContextHolder 관리

5. 실행 흐름

  1. 요청 들어옴
  2. Filter에서 토큰/세션 확인
  3. 인증 정보 SecurityContext에 저장
  4. Controller에서 권한 체크
  5. 통과하면 비즈니스 로직 실행

핵심: Filter -> Authentication -> Authorization 순서로 보안처리