[JWT] JWT토큰 메서드간의 관계

Oksun Noh·2025년 2월 2일
0

그들이 어떤 값을 받고, 저장? 하는지에 대한 관계를 설명

1.전체적인 관계 요약

  • SecurityContextHolder → 현재 로그인한 사용자 정보를 전역적으로 저장
  • SecurityContext → 현재 요청의 인증 정보를 저장
  • Authentication → 사용자의 인증 정보(아이디, 비밀번호, 권한 등) 저장
  • principal → 현재 로그인한 사용자 정보 (UserDetails)
  • UserDetails → Spring Security에서 제공하는 사용자 정보 인터페이스
  • User → UserDetails의 기본 구현체

즉, Spring Security 기본 설정에서는 principal == UserDetails 이므로 principal.getUsername()을 호출할 수 있음!

  1. 쉽게 댑스로 구분하면 아래와 같음
  • SecurityContextHolder
    • SecurityContest
      • authentication
        • principal
          • userDetails

User는 userDetails를 객체화 시킨 것이라고 생각하면 됨

SecurityContext securityContext = SecurityContextHolder.getContext(); // 1. 전역 보안 컨텍스트 가져오기
Authentication authentication = securityContext.getAuthentication(); // 2. 현재 로그인한 사용자의 인증 정보 가져오기
Object principal = authentication.getPrincipal(); // 3. principal (UserDetails) 가져오기

if (principal instanceof UserDetails) {
    UserDetails userDetails = (UserDetails) principal;
    System.out.println("사용자 이름: " + userDetails.getUsername());
    System.out.println("사용자 권한: " + userDetails.getAuthorities());
}
profile
저는 만두를 좋아합니다

0개의 댓글