From 99bb59ddea36e2da5250ad0003f02868ca10f280 Mon Sep 17 00:00:00 2001 From: Jin Hyuk Date: Tue, 8 Oct 2024 12:48:54 +0900 Subject: [PATCH] =?UTF-8?q?feat/#507:=20=EC=A4=91=EB=B3=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20reissueToken=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/auth/AuthService.java | 31 +++++++++++++++++-- .../common/security/domain/JwtManager.java | 29 ++--------------- .../common/security/domain/JwtResolver.java | 8 +++-- 3 files changed, 36 insertions(+), 32 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 f95d6d47d..fd6aae380 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,6 +1,8 @@ package com.gaebaljip.exceed.application.service.auth; -import com.gaebaljip.exceed.common.EatCeedStaticMessage; +import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter; +import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException; +import com.gaebaljip.exceed.common.security.domain.JwtResolver; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -9,6 +11,7 @@ import com.gaebaljip.exceed.application.domain.member.MemberEntity; import com.gaebaljip.exceed.application.port.in.auth.AuthUsecase; import com.gaebaljip.exceed.application.port.out.member.MemberPort; +import com.gaebaljip.exceed.common.EatCeedStaticMessage; import com.gaebaljip.exceed.common.dto.HttpRequestDTO; import com.gaebaljip.exceed.common.dto.TokenDTO; import com.gaebaljip.exceed.common.exception.auth.PasswordMismatchException; @@ -25,6 +28,8 @@ public class AuthService implements AuthUsecase { private final MemberPort memberPort; private final BCryptPasswordEncoder bCryptPasswordEncoder; private final JwtManager jwtManager; + private final JwtResolver jwtResolver; + private final RedisAdapter redisAdapter; @Override public TokenDTO execute(LoginRequest request) { @@ -37,7 +42,9 @@ public TokenDTO execute(LoginRequest request) { .accessToken(jwtManager.generateAccessToken(member.getId())) .refreshToken(jwtManager.generateRefreshToken(member.getId())) .build(); - jwtManager.saveRefreshToken(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + member.getId().toString(), tokenDTO.refreshToken()); + jwtManager.saveRefreshToken( + EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + member.getId().toString(), + tokenDTO.refreshToken()); return tokenDTO; } @@ -45,7 +52,25 @@ public TokenDTO execute(LoginRequest request) { public TokenDTO reIssueToken( String accessToken, String refreshToken, HttpRequestDTO requestDTO) { if (jwtManager.validateRefreshToken(refreshToken, requestDTO)) { - return jwtManager.reissueToken(accessToken); + return reissueToken(accessToken); + } + throw InvalidJwtException.EXECPTION; + } + + private TokenDTO reissueToken(String requestAccessToken) { + String accessToken = jwtResolver.extractToken(requestAccessToken); + String accessTokenMemberId = jwtResolver.getMemberIdFromToken(accessToken); + String refreshToken = + redisAdapter + .query(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + accessTokenMemberId) + .orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION); + String refreshTokenMemberId = jwtResolver.getMemberIdFromToken(refreshToken); + + if (accessTokenMemberId.equals(refreshTokenMemberId)) { + return TokenDTO.builder() + .accessToken(jwtManager.generateAccessToken(Long.parseLong(accessTokenMemberId))) + .refreshToken(jwtManager.generateRefreshToken(Long.parseLong(refreshTokenMemberId))) + .build(); } throw InvalidJwtException.EXECPTION; } 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 fc4071e26..0e6dbdb3c 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,12 +6,12 @@ import javax.servlet.http.HttpServletRequest; -import com.gaebaljip.exceed.common.EatCeedStaticMessage; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Component; import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter; +import com.gaebaljip.exceed.common.EatCeedStaticMessage; import com.gaebaljip.exceed.common.dto.HttpRequestDTO; import com.gaebaljip.exceed.common.dto.TokenDTO; import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException; @@ -27,7 +27,7 @@ @Component @Slf4j public class JwtManager { - private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60; // 3일 + private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 30; // 30분 private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7일 private final Key key; private RedisAdapter redisAdapter; @@ -163,29 +163,4 @@ public boolean validateRefreshToken(String refreshToken, HttpRequestDTO requestD public void saveRefreshToken(String memberId, String refreshToken) { redisAdapter.saveWithExpiration(memberId, refreshToken, REFRESH_TOKEN_EXPIRE_TIME); } - - public Claims parseClaims(String Token) { - try { - return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody(); - } catch (io.jsonwebtoken.ExpiredJwtException e) { - return e.getClaims(); - } - } - - public TokenDTO reissueToken(String accessToken) { - String accessTokenMemberId = parseClaims(accessToken).getSubject(); - String refreshToken = - redisAdapter - .query(EatCeedStaticMessage.REDIS_REFRESH_TOKEN_KEY + accessTokenMemberId) - .orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION); - String refreshTokenMemberId = parseClaims(refreshToken).getSubject(); - - if (accessTokenMemberId.equals(refreshTokenMemberId)) { - return TokenDTO.builder() - .accessToken(generateAccessToken(Long.parseLong(accessTokenMemberId))) - .refreshToken(generateRefreshToken(Long.parseLong(refreshTokenMemberId))) - .build(); - } - throw InvalidJwtException.EXECPTION; - } } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtResolver.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtResolver.java index 36f4e47fb..7e80b58b0 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtResolver.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtResolver.java @@ -27,8 +27,12 @@ public String getMemberIdFromToken(String token) { return parseClaims(token).getSubject(); } - private Claims parseClaims(String Token) { - return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody(); + public Claims parseClaims(String Token) { + try { + return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(Token).getBody(); + } catch (io.jsonwebtoken.ExpiredJwtException e) { + return e.getClaims(); + } } public String extractToken(String bearerToken) {