-
Notifications
You must be signed in to change notification settings - Fork 16
[김찬호] Sprint10 #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 김찬호
Are you sure you want to change the base?
[김찬호] Sprint10 #144
Conversation
| @Value("${jwt.refresh-token-expiration-minutes}") | ||
| private int refreshTokenExpirationMinutes; | ||
|
|
||
| public String generateAccessToken(Map<String, Object> claims, String subject) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런식으로 claim 정보나 subject 정보 자체를 넘기느 방식은 조금 위험할수있습니다.
|
|
||
| @Override | ||
| public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
| Authentication authentication) throws IOException, ServletException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
내부에서 401 에러나 500에러도 핸들링 되도록 구현해보시면 좋을것 같아요!
| filterChain.doFilter(request, response); | ||
| } | ||
|
|
||
| private Map<String, Object> verifyJws(HttpServletRequest request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 검증하는 로직이 JwtTokenProvider 에서 구현되면 좀더 책임분리가 잘된 코드가 됩니다.
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| public class JwtAuthenticationFilter extends OncePerRequestFilter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필터를 주입하는 부분이 보이지 않는것 같아요! 확인해주세요.
요구사항
기본
JWT 컴포넌트 구현
리팩토링 - 로그인
미션 9와 마찬가지로 Spring Security의 formLogin + 미션 9의 인증 흐름은 그대로 유지하면서 필요한 부분만 대체합니다.
AuthenticationSuccessHandler 컴포넌트를 대체하세요.
기존 구현체는 LoginSuccessHandler입니다.
JwtLoginSuccessHandler를 정의하고 대체하세요.
JWT 인증 필터 구현
요청 당 한번만 실행되도록 OncePerRequestFilter를 상속하세요.
요청 헤더(Authorization)에 Bearer 토큰이 포함된 경우에만 인증을 시도하세요.
JwtProvider를 통해 엑세스 토큰의 유효성을 검사하세요.
유효한 토큰인 경우 UsernamePasswordAuthenticationToken 객체를 활용해 인증 완료 처리하세요.
리프레시 토큰을 활용한 엑세스 토큰 재발급
리프레시 토큰을 활용해 엑세스 토큰을 재발급하는 API를 구현하세요.
API 스펙
permitAll 설정에 포함하세요.
토큰 재발급 API로 대체할 수 있는 컴포넌트를 모두 삭제하세요.
Me API (GET /auth/me)
RememberMe
리팩토링 - 로그아웃
심화
리팩토링 - 토큰 상태 관리
토큰 기반 인증 방식은 세션 기반 인증 방식과 달리 무상태(stateless)이기 때문에 사용자의 로그인 상태를 제어하기 어렵습니다.
따라서 SessionRegistry를 통해 세션의 상태를 관리했던 것처럼, JWT의 상태를 관리할 수 있는 컴포넌트를 추가해야합니다.
메모리에 JwtInformation을 저장하는 JwtRegistry 구현체입니다.
동시성 처리를 위해 다음과 같이 구성하세요. 동시성에 대해서는 다음 미션에서 학습합니다.
JwtRegistry를 활용해 동시 로그인 제한 기능을 리팩토링하세요.
동일한 계정으로 로그인 시 기존 로그인 세션을 무효화합니다.
주기적으로 만료된 토큰 정보를 레지스트리에서 삭제하세요.
@EnableScheduling를 추가하세요.
주요 변경사항
스크린샷
멘토에게