From c554db8f19b6ba75e9be09fab8c364ac3404aed5 Mon Sep 17 00:00:00 2001 From: aeeazip Date: Wed, 12 Jul 2023 20:39:25 +0900 Subject: [PATCH] =?UTF-8?q?fix(#5)=20:=20DB=EC=97=90=20refreshToken=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/auth/OAuthController.java | 10 ++--- .../trothcam/domain/member/Member.java | 14 +++++++ .../trothcam/service/auth/OAuthService.java | 42 ++++++++++--------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/main/java/trothly/trothcam/controller/auth/OAuthController.java b/src/main/java/trothly/trothcam/controller/auth/OAuthController.java index d2d19a3..831fc10 100644 --- a/src/main/java/trothly/trothcam/controller/auth/OAuthController.java +++ b/src/main/java/trothly/trothcam/controller/auth/OAuthController.java @@ -42,11 +42,11 @@ public BaseResponse appleLogin(@RequestBody @Validated LoginReqDto } // refreshToken으로 accessToken 재발급 -// @PostMapping("/refresh") -// public BaseResponse regenerateAccessToken(@RequestBody @Validated RefreshTokenReqDto refreshTokenReqDto) throws BaseException { -// LoginResDto result = oauthService.regenerateAccessToken(refreshTokenReqDto); -// return BaseResponse.onSuccess(result); -// } + @PostMapping("/refresh") + public BaseResponse regenerateAccessToken(@RequestBody @Validated RefreshTokenReqDto refreshTokenReqDto) throws BaseException { + LoginResDto result = oauthService.regenerateAccessToken(refreshTokenReqDto); + return BaseResponse.onSuccess(result); + } // 구글 로그인 @GetMapping(value="/auth/{socialLoginType}") diff --git a/src/main/java/trothly/trothcam/domain/member/Member.java b/src/main/java/trothly/trothcam/domain/member/Member.java index 9e81008..2e50d57 100644 --- a/src/main/java/trothly/trothcam/domain/member/Member.java +++ b/src/main/java/trothly/trothcam/domain/member/Member.java @@ -8,6 +8,7 @@ import javax.persistence.*; import javax.validation.constraints.Email; +import java.time.LocalDateTime; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -33,9 +34,22 @@ public class Member extends BaseTimeEntity { @Enumerated(EnumType.STRING) private Provider provider; + @Column(name = "refresh_token", nullable = false) + private String refreshToken; + + @Column(name = "refresh_token_expires_at", nullable = false) + private LocalDateTime refreshTokenExpiresAt; + @Builder private Member(String email, Provider provider) { this.email = email; this.provider = provider; + this.refreshToken = ""; + this.refreshTokenExpiresAt = LocalDateTime.now(); + } + + public void updateRefreshToken(String refreshToken) { + this.refreshToken = refreshToken; + this.refreshTokenExpiresAt = LocalDateTime.now(); } } diff --git a/src/main/java/trothly/trothcam/service/auth/OAuthService.java b/src/main/java/trothly/trothcam/service/auth/OAuthService.java index deb2067..2dbf379 100644 --- a/src/main/java/trothly/trothcam/service/auth/OAuthService.java +++ b/src/main/java/trothly/trothcam/service/auth/OAuthService.java @@ -12,6 +12,7 @@ import trothly.trothcam.dto.auth.apple.LoginReqDto; import trothly.trothcam.dto.auth.apple.LoginResDto; //import trothly.trothcam.dto.auth.apple.RefreshTokenReqDto; +import trothly.trothcam.dto.auth.apple.RefreshTokenReqDto; import trothly.trothcam.dto.auth.google.GoogleOauthToken; import trothly.trothcam.dto.auth.google.GoogleUser; import trothly.trothcam.exception.base.*; @@ -19,6 +20,7 @@ import trothly.trothcam.auth.apple.AppleOAuthUserProvider; import trothly.trothcam.domain.member.*; //import trothly.trothcam.exception.custom.InvalidTokenException; +import trothly.trothcam.exception.custom.InvalidTokenException; import trothly.trothcam.service.JwtService; import javax.servlet.http.HttpServletResponse; @@ -76,29 +78,31 @@ public LoginResDto appleLogin(LoginReqDto loginReqDto) throws BaseException { // redisTemplate.opsForValue().set(member.getId().toString(), newRefreshToken, 14L, TimeUnit.SECONDS); // log.info("redis에 저장된 refreshToken : " + newRefreshToken + "\nmember.getId : " + member.getId().toString()); + // DB에 refreshToken 저장 + member.updateRefreshToken(newRefreshToken); + memberRepository.save(member); return new LoginResDto(newAccessToken, newRefreshToken); } // refreshToken으로 accessToken 발급하기 -// @Transactional -// public LoginResDto regenerateAccessToken(RefreshTokenReqDto refreshTokenReqDto) throws BaseException { -// String getRefreshToken = refreshTokenReqDto.getRefreshToken(); -// Long memberId = jwtService.getMemberIdFromJwtToken(getRefreshToken); -// -// String redisRefreshToken = redisTemplate.opsForValue().get(memberId.toString()); -// log.info("getRefreshToken : " + getRefreshToken); -// log.info("redisRefreshToken : " + redisRefreshToken); // 요 부분이 값이 있었다가 null로 떴다가 그래 -// -// if(!getRefreshToken.equals(redisRefreshToken)) -// throw new InvalidTokenException("유효하지 않은 Refresh Token입니다."); -// -// String newAccessToken = jwtService.encodeJwtToken(new TokenDto(memberId)); -// String newRefreshToken = jwtService.encodeJwtRefreshToken(memberId); -// -// redisTemplate.opsForValue().set(memberId.toString(), newRefreshToken, 14L, TimeUnit.SECONDS); -// -// return new LoginResDto(newAccessToken, newRefreshToken); -// } + @Transactional + public LoginResDto regenerateAccessToken(RefreshTokenReqDto refreshTokenReqDto) throws BaseException { + Long memberId = jwtService.getMemberIdFromJwtToken(refreshTokenReqDto.getRefreshToken()); + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new IllegalArgumentException("해당되는 member_id를 찾을 수 없습니다.")); + + String refreshToken = refreshTokenReqDto.getRefreshToken(); + if(refreshToken.equals(member.getRefreshToken())) + throw new InvalidTokenException("유효하지 않은 Refresh Token입니다."); + + String newRefreshToken = jwtService.encodeJwtRefreshToken(memberId); + String newAcessToken = jwtService.encodeJwtToken(new TokenDto(memberId)); + + member.updateRefreshToken(newRefreshToken); + memberRepository.save(member); + + return new LoginResDto(newAcessToken, newRefreshToken); + } // 구글 로그인 //1. request