Closed clap-0 closed 8 months ago
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); }
또한 기존의 로그인 이후 응답 DTO인 LoginResponse.java 클래스에서 refreshToken을 제외시켰습니다.
이를 통해 Body에 리프레시 토큰을 포함시키는 방식보다 보안이 더 안전할 것으로 기대됩니다.
@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)); }
에러 유형을 정의하는 ErrorResult 클래스와, 에러를 전역적으로 처리하는 GlobalExceptionHandler 클래스를 구현했습니다.
이전에 하던 방식과 달리 도메인 별로 예외 클래스를 만들지 않고, 일반적인 예외 상황이라면 GeneralException 클래스를 던지도록 하였습니다.
@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; } }
🔥 Related Issues
💜 작업 내용
✅ PR Point
HttpOnly 쿠키에 리프레시 토큰 저장
또한 기존의 로그인 이후 응답 DTO인 LoginResponse.java 클래스에서 refreshToken을 제외시켰습니다.
이를 통해 Body에 리프레시 토큰을 포함시키는 방식보다 보안이 더 안전할 것으로 기대됩니다.
액세스 토큰 재발급 구현
예외 처리를 위한 클래스
에러 유형을 정의하는 ErrorResult 클래스와, 에러를 전역적으로 처리하는 GlobalExceptionHandler 클래스를 구현했습니다.
이전에 하던 방식과 달리 도메인 별로 예외 클래스를 만들지 않고, 일반적인 예외 상황이라면 GeneralException 클래스를 던지도록 하였습니다.
☀ 스크린샷 / GIF / 화면 녹화