Spring Security + JWT 구현 - 1(토큰 발급)

선종우·2023년 5월 28일
0

1. 공부배경

  • 진행중인 프로젝트에서 Spring Security를 사용 중인데 JWT를 Spring Security 안에 녹여내는 데 어려움이 많았다.
  • JWT 토큰 발급 및 인증 과정 자체는 어려운 게 아니었으나 이를 Spring Security에 적용하는 게 쉽지 않았다. 이번 기회에 Spring Security의 작동 과정을 이해하고 JWT를 적용하고자 공부를 진행했다.

2. 공부내용

  • 우선 이번 글에서는 Spring Security의 로그인 로직을 그대로 사용하면서, 로그인 성공 시 JWT토큰을 발급하는 과정을 정리하였다. 큰 틀은 다음과 같다.
  1. Id/password 기반 로그인 및 jwt 토큰을 발급하는 Custom Filter구현(JwtAuthenticationFilter)
    a. CustomFilter에는 attempAuthentication()(id/password 이용 인증)과 successfulAuthentication()(인증 시 응답값 작성)을 override하여야 한다.
  2. JwtAuthenticationFilter가 등록된 SecurityFilterChain Bean 등록

2-1. 로그인 및 JWT 토큰 발급 과정 개요도


0개의 댓글