Skip to content

Commit

Permalink
feat: JWT 필터 Access Token 검증하도록 변경 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 5, 2023
1 parent 184521c commit 74c8174
Showing 1 changed file with 7 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
return;
}

// 사용자 요청 헤더에서 RefreshToken 추출-> RefreshToken이 없거나 유효하지 않다면 null
String refreshToken = jwtService.extractRefreshToken(request)
.filter(jwtService::isTokenValid)
.orElse(null);

// 리프레시 토큰이 요청 헤더에 존재하고 유효하다면, AccessToken이 만료된 것 -> AccessToken 재발급
if (refreshToken != null) {
String email = jwtService.extractEmail(refreshToken).orElseThrow(() -> new TokenException(ErrorCode.INVALID_TOKEN));
if (isRefreshTokenMatch(email, refreshToken)) {
String newAccessToken = jwtService.createAccessToken(email);
String newRefreshToken = jwtService.createRefreshToken(email);
jwtService.updateRefreshToken(email, newRefreshToken);
jwtService.sendAccessAndRefreshToken(response, newAccessToken, refreshToken);
}
return;
}
log.info("JwtAuthenticationProcessingFilter 호출");
String accessToken = jwtService.extractAccessToken(request).orElse(null);

// AccessToken을 검사하고 인증 처리
// AccessToken이 없거나 유효하지 않다면, 인증 객체가 담기지 않은 상태로 다음 필터로 넘어가기 때문에 403 에러 발생
// AccessToken이 유효하다면, 인증 객체가 담긴 상태로 다음 필터로 넘어가기 때문에 인증 성공
else {
checkAccessTokenAndAuthentication(request, response, filterChain);
if (jwtService.isTokenValid(accessToken)) {
jwtService.extractEmail(accessToken)
.ifPresent(email -> memberRepository.findByEmail(email)
.ifPresent(this::saveAuthentication));
}
filterChain.doFilter(request, response);
}

public boolean isRefreshTokenMatch(String email, String refreshToken) {
Expand Down

0 comments on commit 74c8174

Please sign in to comment.