웹 개발

id vs 인증 사용자 정보 차이

Blue_bull 2025. 4. 24. 10:21

 


정리: @AuthenticationPrincipal vs @PathVariable id

구분 목적 방식 사용 예

@PathVariable Long id 특정 리소스(DB 행) 식별 URL로 직접 id 받음 GET /api/quizzes/{id}
@AuthenticationPrincipal UserDetails 현재 로그인한 사용자 정보 JWT에서 email 등 추출해서 자동 주입 DELETE /api/auth/profile

핵심 포인트

  • "본인 정보"를 조회/수정/삭제할 때는 @AuthenticationPrincipal로 email이나 username을 받아서 사용
  • 받은 정보로 DTO의 email, userId, role 등을 채우고 DB에서 findByEmail()이나 findByUserId()로 사용자 데이터 조회

예시 흐름

@DeleteMapping("/profile")
public ResponseEntity<?> deleteMyAccount(@AuthenticationPrincipal UserDetails userDetails) {
    String email = userDetails.getUsername(); // JWT에서 추출됨
    memberService.deleteByEmail(email); // email 기준으로 사용자 삭제
    return ResponseEntity.ok("탈퇴 완료");
}
  • URL에 ID 없음
  • JWT 토큰에서 자동 추출된 사용자 정보
  • 서비스 계층에서도 ID 없이 email 기반 처리