From 653d2c5647cfc3093fcdde97c0dcb1ca6ee0d03e Mon Sep 17 00:00:00 2001 From: Jin Hyuk Date: Tue, 24 Sep 2024 18:50:09 +0900 Subject: [PATCH] =?UTF-8?q?feat/#507:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=ED=95=A0=20=EB=95=8C=20redis=EC=97=90=20=EB=A6=AC=ED=94=84?= =?UTF-8?q?=EB=A0=88=EC=8B=9C=20=ED=86=A0=ED=81=B0=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exceed/application/service/auth/AuthService.java | 8 +++++++- .../exceed/common/security/domain/JwtManager.java | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java index 7899290db..ab12e54ed 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java @@ -1,5 +1,7 @@ package com.gaebaljip.exceed.application.service.auth; +import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter; +import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -14,6 +16,8 @@ import lombok.RequiredArgsConstructor; +import javax.servlet.http.HttpServletRequest; + @Service @RequiredArgsConstructor @Transactional(readOnly = true) @@ -29,9 +33,11 @@ public LoginResponseDTO execute(LoginRequest request) { if (!bCryptPasswordEncoder.matches(request.password(), member.getPassword())) { throw PasswordMismatchException.EXECPTION; } - return LoginResponseDTO.builder() + LoginResponseDTO loginResponseDTO = LoginResponseDTO.builder() .accessToken(jwtManager.generateAccessToken(member.getId())) .refreshToken(jwtManager.generateRefreshToken(member.getId())) .build(); + jwtManager.saveRefreshToken(request.email(), loginResponseDTO.refreshToken()); + return loginResponseDTO; } } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java index 49395d7d9..d4c04d2ac 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java @@ -6,6 +6,8 @@ import javax.servlet.http.HttpServletRequest; +import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Component; @@ -25,10 +27,12 @@ public class JwtManager { private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 3; // 3일 private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7일 private final Key key; + private RedisAdapter redisAdapter; - public JwtManager(@Value("${jwt.secret}") String secretKey) { + public JwtManager(@Value("${jwt.secret}") String secretKey, RedisAdapter redisAdapter) { byte[] keyBytes = Decoders.BASE64.decode(secretKey); this.key = Keys.hmacShaKeyFor(keyBytes); + this.redisAdapter = redisAdapter; } public String generateAccessToken(Long memberId) { @@ -152,4 +156,8 @@ public boolean validateRefreshToken(String refreshToken, HttpServletRequest requ } return false; } + + public void saveRefreshToken(String email, String refreshToken) { + redisAdapter.saveWithExpiration(email, refreshToken, REFRESH_TOKEN_EXPIRE_TIME); + } }