Skip to content

Commit

Permalink
토큰 재발급 로직 리팩토링 (#827)
Browse files Browse the repository at this point in the history
* feat: 캐시에서 토큰 값을 조회하여 재발급하는 로직 구현

* feat: 에러 메세지 추가

* chore: submodule 최신화
  • Loading branch information
jschoi-96 authored Dec 24, 2024
1 parent b005fa8 commit 6b10da0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/main/java/balancetalk/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum ErrorCode {
NOT_FOUND_TEMP_TALK_PICK(NOT_FOUND, "임시 저장한 톡픽이 존재하지 않습니다."),
NOT_FOUND_NOTIFICATION(NOT_FOUND, "존재하지 않는 알림입니다."),
NOT_FOUND_GAME_OPTION(NOT_FOUND, "게임 선택지가 존재하지 않습니다."),
NOT_FOUND_CACHE_VALUE(NOT_FOUND, "캐시 값이 존재하지 않습니다."),

// 409
ALREADY_VOTE(CONFLICT, "이미 투표한 게시글입니다."),
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/balancetalk/member/application/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package balancetalk.member.application;

import static balancetalk.global.caffeine.CacheType.RefreshToken;
import static balancetalk.global.exception.ErrorCode.ALREADY_REGISTERED_EMAIL;
import static balancetalk.global.exception.ErrorCode.ALREADY_REGISTERED_NICKNAME;
import static balancetalk.global.exception.ErrorCode.AUTHENTICATION_REQUIRED;
import static balancetalk.global.exception.ErrorCode.CACHE_NOT_FOUND;
import static balancetalk.global.exception.ErrorCode.FORBIDDEN_MEMBER_DELETE;
import static balancetalk.global.exception.ErrorCode.MISMATCHED_EMAIL_OR_PASSWORD;
import static balancetalk.global.exception.ErrorCode.NOT_FOUND_CACHE_VALUE;
import static balancetalk.global.exception.ErrorCode.NOT_FOUND_FILE;
import static balancetalk.global.exception.ErrorCode.NOT_FOUND_MEMBER;
import static balancetalk.global.exception.ErrorCode.PASSWORD_MISMATCH;
Expand All @@ -15,7 +17,6 @@
import balancetalk.file.domain.FileHandler;
import balancetalk.file.domain.FileType;
import balancetalk.file.domain.repository.FileRepository;
import balancetalk.global.caffeine.CacheType;
import balancetalk.global.exception.BalanceTalkException;
import balancetalk.global.jwt.JwtTokenProvider;
import balancetalk.member.domain.Member;
Expand All @@ -26,12 +27,13 @@
import balancetalk.member.dto.MemberDto.MemberResponse;
import balancetalk.member.dto.MemberDto.MemberUpdateRequest;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -85,7 +87,7 @@ public String login(final LoginRequest loginRequest, HttpServletResponse respons
String accessToken = jwtTokenProvider.createAccessToken(authentication, member.getId());
String refreshToken = jwtTokenProvider.createRefreshToken(authentication, member.getId());

Optional.ofNullable(cacheManager.getCache(CacheType.RefreshToken.getCacheName()))
Optional.ofNullable(cacheManager.getCache(RefreshToken.getCacheName()))
.ifPresentOrElse(
cache -> cache.put(member.getId(), refreshToken),
() -> {
Expand Down Expand Up @@ -152,17 +154,16 @@ public void verifyNickname(String nickname) {
}
}

public String reissueAccessToken(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if (name.equals("refreshToken")) {
String refreshToken = cookie.getValue();
jwtTokenProvider.validateToken(refreshToken);
return jwtTokenProvider.reissueAccessToken(refreshToken);
}
public String reissueAccessToken(ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
Cache cache = Optional.ofNullable(cacheManager.getCache(RefreshToken.getCacheName()))
.orElseThrow(() -> new BalanceTalkException(CACHE_NOT_FOUND));
ValueWrapper valueWrapper = cache.get(member.getId());
if (valueWrapper == null) {
throw new BalanceTalkException(NOT_FOUND_CACHE_VALUE);
}
return null;
String refreshToken = (String) valueWrapper.get();
return jwtTokenProvider.reissueAccessToken(refreshToken);
}

public void updateMemberInformation(MemberUpdateRequest memberUpdateRequest, ApiMember apiMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -74,8 +73,8 @@ public void verifyNickname(@RequestParam @NotBlank

@GetMapping("/reissue")
@Operation(summary = "액세스 토큰 재발급", description = "만료된 액세스 토큰을 재발급 받는다.")
public String reissueAccessToken(HttpServletRequest request) {
return memberService.reissueAccessToken(request);
public String reissueAccessToken(@Parameter(hidden = true) @AuthPrincipal ApiMember apiMember) {
return memberService.reissueAccessToken(apiMember);
}

@PutMapping
Expand Down

0 comments on commit 6b10da0

Please sign in to comment.