From 77d7fe1fae222bc6ebdbafd2472d61516bd8de1c Mon Sep 17 00:00:00 2001 From: JeongHeumChoi Date: Wed, 8 May 2024 17:28:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Chore:=20Credentials=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spot-server-properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spot-server-properties b/spot-server-properties index 88515af..a440066 160000 --- a/spot-server-properties +++ b/spot-server-properties @@ -1 +1 @@ -Subproject commit 88515afcdc708913af815f585bce9affa47980ab +Subproject commit a440066410fac97d04913f4e9eb36b60e199565f From cc98db3ff3728f47fc42823db4f1028d367515f3 Mon Sep 17 00:00:00 2001 From: JeongHeumChoi Date: Wed, 8 May 2024 21:12:36 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Feat:=20Reissue=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ice/spot/controller/AuthController.java | 29 +++++++++++++++++++ .../java/ice/spot/service/AuthService.java | 14 +++++++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/ice/spot/controller/AuthController.java b/src/main/java/ice/spot/controller/AuthController.java index 173b2a9..49153ae 100644 --- a/src/main/java/ice/spot/controller/AuthController.java +++ b/src/main/java/ice/spot/controller/AuthController.java @@ -1,11 +1,20 @@ package ice.spot.controller; import ice.spot.annotation.UserId; +import ice.spot.constant.Constants; import ice.spot.dto.global.ResponseDto; import ice.spot.dto.request.OauthSignUpDto; +import ice.spot.dto.response.JwtTokenDto; +import ice.spot.exception.CommonException; +import ice.spot.exception.ErrorCode; import ice.spot.service.AuthService; +import ice.spot.util.CookieUtil; +import ice.spot.util.HeaderUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +26,9 @@ @RequiredArgsConstructor public class AuthController { + @Value("${server.domain}") + private String domain; + private final AuthService authService; @PostMapping("/oauth2/sign-up") @@ -24,4 +36,21 @@ public ResponseDto signUp(@UserId Long userId, @RequestBody OauthSignUpDto oa authService.signUp(userId, oauthSignUpDto); return ResponseDto.ok(null); } + + @PostMapping("/auth/reissue") + public ResponseDto reissue( + HttpServletRequest request, + HttpServletResponse response, + @UserId Long userId){ + log.info("controller 진입 성공"); + String refreshToken = HeaderUtil.refineHeader(request, Constants.PREFIX_AUTH, Constants.PREFIX_BEARER) + .orElseThrow(() -> new CommonException(ErrorCode.INVALID_HEADER_VALUE)); + log.info("헤더값 조회 성공"); + JwtTokenDto jwtTokenDto = authService.reGenerateTokens(userId, refreshToken); + + CookieUtil.addCookie(response, domain, Constants.ACCESS_COOKIE_NAME, jwtTokenDto.accessToken()); + CookieUtil.addSecureCookie(response, domain, Constants.REFRESH_COOKIE_NAME, jwtTokenDto.refreshToken(), 60 * 60 * 24 * 14); + + return ResponseDto.ok(jwtTokenDto); + } } diff --git a/src/main/java/ice/spot/service/AuthService.java b/src/main/java/ice/spot/service/AuthService.java index e4a89bc..4b7d553 100644 --- a/src/main/java/ice/spot/service/AuthService.java +++ b/src/main/java/ice/spot/service/AuthService.java @@ -2,9 +2,11 @@ import ice.spot.domain.User; import ice.spot.dto.request.OauthSignUpDto; +import ice.spot.dto.response.JwtTokenDto; import ice.spot.exception.CommonException; import ice.spot.exception.ErrorCode; import ice.spot.repository.UserRepository; +import ice.spot.util.JwtUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -16,6 +18,7 @@ public class AuthService { private final UserRepository userRepository; + private final JwtUtil jwtUtil; @Transactional public void signUp(Long userId, OauthSignUpDto oauthSignUpDto){ @@ -25,4 +28,15 @@ public void signUp(Long userId, OauthSignUpDto oauthSignUpDto){ oauthUser.register(oauthSignUpDto.nickname()); } + @Transactional + public JwtTokenDto reGenerateTokens(Long userId, String refreshToken){ + log.info("re generate tokens 진입성공"); + User loginUser = userRepository.findByIdAndRefreshToken(userId, refreshToken) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER)); + log.info("유저 조회 성공"); + JwtTokenDto jwtTokenDto = jwtUtil.generateTokens(loginUser.getId(), loginUser.getRole()); + + loginUser.updateRefreshToken(jwtTokenDto.refreshToken()); + return jwtTokenDto; + } } From d9a90e9c47587385416a9c52f519f8799ddb6c5f Mon Sep 17 00:00:00 2001 From: JeongHeumChoi Date: Wed, 8 May 2024 21:13:16 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Chore:=20Credentials=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spot-server-properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spot-server-properties b/spot-server-properties index a440066..0626579 160000 --- a/spot-server-properties +++ b/spot-server-properties @@ -1 +1 @@ -Subproject commit a440066410fac97d04913f4e9eb36b60e199565f +Subproject commit 062657935b3be59a83309b7ab827391e69105bc6