Skip to content

Commit 12e2396

Browse files
committed
Fix: DB/JWT 시간 통일 및 Access/Refresh Token 타입 Claim 분리
1 parent 031c85e commit 12e2396

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

‎src/main/java/com/ureca/unity/domain/auth/model/RefreshToken.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.Getter;
66
import lombok.NoArgsConstructor;
77

8-
import java.time.LocalDateTime;
8+
import java.time.Instant;
99

1010
@Getter
1111
@Builder
@@ -16,5 +16,5 @@ public class RefreshToken {
1616
private Long id;
1717
private Long userId;
1818
private String token;
19-
private LocalDateTime expiresAt;
19+
private Instant expiresAt;
2020
}

‎src/main/java/com/ureca/unity/domain/auth/service/RefreshTokenServiceImpl.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.stereotype.Service;
1919
import org.springframework.transaction.annotation.Transactional;
2020

21-
import java.time.LocalDateTime;
21+
import java.time.Instant;
2222

2323
@Service
2424
@RequiredArgsConstructor
@@ -42,7 +42,7 @@ public void saveRefreshToken(Long userId, String refreshToken) {
4242
.userId(userId)
4343
.token(refreshToken)
4444
.expiresAt(
45-
LocalDateTime.now()
45+
Instant.now()
4646
.plusSeconds(jwtProperties.refreshExpirationSeconds())
4747
)
4848
.build()
@@ -60,7 +60,7 @@ public TokenResponse refreshAccessToken(HttpServletRequest request,
6060
// 1-1. JWT 서명 검증 (DB 조회 전)
6161
Long jwtUserId;
6262
try {
63-
jwtUserId = jwtProvider.getUserId(refreshToken);
63+
jwtUserId = jwtProvider.getUserId(refreshToken, "refresh");
6464
} catch (JwtException | IllegalArgumentException e) {
6565
throw new CustomException(ErrorCode.REFRESH_TOKEN_INVALID);
6666
}
@@ -76,7 +76,7 @@ public TokenResponse refreshAccessToken(HttpServletRequest request,
7676
}
7777

7878
// 3. 만료 체크
79-
if (savedToken.getExpiresAt().isBefore(LocalDateTime.now())) {
79+
if (savedToken.getExpiresAt().isBefore(Instant.now())) {
8080
// 만료된 토큰은 즉시 삭제 후 예외 반환 (rotation 로직 미진입)
8181
refreshTokenMapper.deleteByToken(refreshToken); // 만료 토큰 정리
8282
throw new CustomException(ErrorCode.REFRESH_TOKEN_EXPIRED);

‎src/main/java/com/ureca/unity/global/security/JwtIssuer.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public JwtIssuer(JwtProperties props) {
3232
public TokenResponse issueAccessToken(Long userId) {
3333

3434
long accessExp = props.accessExpirationSeconds();
35-
String accessToken = createToken(userId, accessExp);
35+
String accessToken = createToken(userId, accessExp, "access");
3636

3737
return TokenResponse.builder()
3838
.accessToken(accessToken)
@@ -41,10 +41,10 @@ public TokenResponse issueAccessToken(Long userId) {
4141
}
4242

4343
public String issueRefreshToken(Long userId) {
44-
return createToken(userId, props.refreshExpirationSeconds());
44+
return createToken(userId, props.refreshExpirationSeconds(), "refresh");
4545
}
4646

47-
private String createToken(Long userId, long expSeconds) {
47+
private String createToken(Long userId, long expSeconds, String type) {
4848
if (userId == null) {
4949
throw new IllegalArgumentException("userId must not be null");
5050
}
@@ -54,6 +54,7 @@ private String createToken(Long userId, long expSeconds) {
5454
return Jwts.builder()
5555
.issuer(props.issuer())
5656
.subject(String.valueOf(userId))
57+
.claim("type", type)
5758
.issuedAt(Date.from(now))
5859
.expiration(Date.from(now.plusSeconds(expSeconds)))
5960
.signWith(key, Jwts.SIG.HS256)

‎src/main/java/com/ureca/unity/global/security/JwtProvider.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.ureca.unity.global.security;
22

33
import com.ureca.unity.domain.auth.constant.JwtProperties;
4+
import com.ureca.unity.global.exception.CustomException;
5+
import com.ureca.unity.global.exception.ErrorCode;
46
import io.jsonwebtoken.Claims;
57
import io.jsonwebtoken.Jwts;
68
import io.jsonwebtoken.security.Keys;
@@ -24,18 +26,24 @@ public JwtProvider(JwtProperties props) {
2426
}
2527

2628
/* JWT 검증 + userId(subject) 추출 */
27-
public Long getUserId(String token) {
29+
public Long getUserId(String token, String expectedType) {
2830
Claims claims = Jwts.parser()
2931
.verifyWith(key)
3032
.build()
3133
.parseSignedClaims(token)
3234
.getPayload();
3335

36+
String tokenType = claims.get("type", String.class);
37+
38+
if (!expectedType.equals(tokenType)) {
39+
throw new CustomException(ErrorCode.REFRESH_TOKEN_INVALID);
40+
}
41+
3442
return Long.valueOf(claims.getSubject());
3543
}
3644

3745
public Authentication getAuthentication(String token) {
38-
Long userId = getUserId(token);
46+
Long userId = getUserId(token, "access");
3947

4048
return new UsernamePasswordAuthenticationToken(
4149
userId,

0 commit comments

Comments
 (0)