Igloo-Club / Igloo-Club-BE

1 stars 1 forks source link

[feat] 리프레시 토큰을 통한 재발급 구현 #7

Closed clap-0 closed 8 months ago

clap-0 commented 8 months ago

🔥 Related Issues

💜 작업 내용

✅ PR Point

HttpOnly 쿠키에 리프레시 토큰 저장

public static void addHttpOnlyCookie(HttpServletResponse response, String name, String value, int maxAge) {
    Cookie cookie = new Cookie(name, value);
    cookie.setPath("/");
    cookie.setMaxAge(maxAge);
    cookie.setHttpOnly(true);
    cookie.setSecure(true);
    response.addCookie(cookie);
}

액세스 토큰 재발급 구현

@PostMapping("/api/auth/refresh")
public ResponseEntity<LoginResponse> createNewAccessToken(@CookieValue(value = "refresh_token", required = false) String refreshToken) {
    if (refreshToken == null) {
        throw new GeneralException(TokenErrorResult.REFRESH_TOKEN_NOT_FOUND);
    }

    String newAccessToken = tokenService.createNewAccessToken(refreshToken);

    return ResponseEntity.status(HttpStatus.CREATED)
            .body(new LoginResponse(newAccessToken));
}

예외 처리를 위한 클래스

@Getter
public class GeneralException extends RuntimeException implements CustomException {

    private final ErrorResult errorResult;

    public GeneralException(ErrorResult errorResult) {
        super(errorResult.getMessage());
        this.errorResult = errorResult;
    }

    public GeneralException(ErrorResult errorResult, Throwable cause) {
        super(errorResult.getMessage(), cause);
        this.errorResult = errorResult;
    }
}

☀ 스크린샷 / GIF / 화면 녹화

image