Skip to content

Commit e05c6b8

Browse files
authored
merge : JWT 키 생성 및 검증 로직을 이전 방식으로 롤백
Refactor: JWT 키 생성 및 검증 로직을 이전 방식으로 롤백
2 parents 169d31b + e0889a3 commit e05c6b8

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

src/main/java/org/terning/terningserver/auth/application/AuthService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public TokenReissueResponse reissueAccessToken(String authorizationHeader) {
111111
Long userId = jwtProvider.getUserIdFrom(authorizationHeader);
112112

113113
User user = userRepository.findById(userId)
114-
.orElseThrow(() -> new JwtException(JwtErrorCode.INVALID_JWT_TOKEN));
114+
.orElseThrow(() -> new JwtException(JwtErrorCode.INVALID_TOKEN));
115115

116116
String providedToken = jwtProvider.resolveToken(authorizationHeader);
117117
user.validateRefreshToken(providedToken);

src/main/java/org/terning/terningserver/auth/jwt/JwtProvider.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88
import io.jsonwebtoken.security.Keys;
99
import io.jsonwebtoken.security.SecurityException;
1010
import jakarta.annotation.PostConstruct;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.Date;
13+
import javax.crypto.SecretKey;
1114
import lombok.RequiredArgsConstructor;
1215
import org.springframework.stereotype.Component;
1316
import org.terning.terningserver.auth.dto.Token;
1417
import org.terning.terningserver.auth.jwt.exception.JwtErrorCode;
1518
import org.terning.terningserver.auth.jwt.exception.JwtException;
1619
import org.terning.terningserver.common.config.ValueConfig;
1720

18-
19-
import javax.crypto.SecretKey;
20-
import java.nio.charset.StandardCharsets;
21-
import java.util.Date;
22-
2321
@Component
2422
@RequiredArgsConstructor
2523
public class JwtProvider {
@@ -28,11 +26,12 @@ public class JwtProvider {
2826
private static final String TOKEN_PREFIX = "Bearer ";
2927

3028
private final ValueConfig valueConfig;
29+
3130
private SecretKey secretKey;
3231

3332
@PostConstruct
3433
protected void init() {
35-
secretKey = Keys.hmacShaKeyFor(valueConfig.getSecretKey().getBytes(StandardCharsets.UTF_8));
34+
this.secretKey = Keys.hmacShaKeyFor(valueConfig.getSecretKey().getBytes(StandardCharsets.UTF_8));
3635
}
3736

3837
public Token generateTokens(Long userId) {
@@ -48,10 +47,9 @@ public Token generateAccessToken(Long userId) {
4847

4948
public Long getUserIdFrom(String authorizationHeader) {
5049
String token = resolveToken(authorizationHeader);
51-
5250
Claims claims = parseClaims(token);
53-
5451
Object userIdClaim = claims.get(USER_ID_CLAIM);
52+
5553
if (userIdClaim instanceof Number) {
5654
return ((Number) userIdClaim).longValue();
5755
}
@@ -73,21 +71,39 @@ private String generateToken(Long userId, long expiration) {
7371
.setClaims(claims)
7472
.setIssuedAt(new Date())
7573
.setExpiration(new Date(System.currentTimeMillis() + expiration))
76-
.signWith(secretKey)
74+
.signWith(this.secretKey)
7775
.compact();
7876
}
7977

8078
private Claims parseClaims(String token) {
8179
try {
8280
return Jwts.parserBuilder()
83-
.setSigningKey(secretKey)
81+
.setSigningKey(this.secretKey)
8482
.build()
8583
.parseClaimsJws(token)
8684
.getBody();
87-
} catch (ExpiredJwtException e) {
88-
throw new JwtException(JwtErrorCode.EXPIRED_JWT_TOKEN);
89-
} catch (UnsupportedJwtException | MalformedJwtException | SecurityException | IllegalArgumentException e) {
90-
throw new JwtException(JwtErrorCode.INVALID_JWT_TOKEN);
85+
} catch (Exception e) {
86+
handleJwtException(e);
87+
throw new JwtException(JwtErrorCode.UNEXPECTED_ERROR);
88+
}
89+
}
90+
91+
private void handleJwtException(Exception e) {
92+
if (e instanceof ExpiredJwtException) {
93+
throw new JwtException(JwtErrorCode.EXPIRED_TOKEN);
94+
}
95+
if (e instanceof SecurityException) {
96+
throw new JwtException(JwtErrorCode.SIGNATURE_ERROR);
97+
}
98+
if (e instanceof MalformedJwtException) {
99+
throw new JwtException(JwtErrorCode.MALFORMED_TOKEN);
100+
}
101+
if (e instanceof UnsupportedJwtException) {
102+
throw new JwtException(JwtErrorCode.UNSUPPORTED_TOKEN);
103+
}
104+
if (e instanceof IllegalArgumentException) {
105+
throw new JwtException(JwtErrorCode.EMPTY_TOKEN);
91106
}
107+
throw new JwtException(JwtErrorCode.INVALID_TOKEN);
92108
}
93109
}

src/main/java/org/terning/terningserver/auth/jwt/exception/JwtErrorCode.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@
77
@Getter
88
@AllArgsConstructor
99
public enum JwtErrorCode {
10-
INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 JWT 토큰입니다."),
11-
INVALID_USER_ID(HttpStatus.BAD_REQUEST, "유효하지 않은 userId 값입니다."),
12-
INVALID_USER_ID_TYPE(HttpStatus.BAD_REQUEST, "유효하지 않은 userId 타입입니다."),
13-
INVALID_USER_DETAILS_TYPE(HttpStatus.INTERNAL_SERVER_ERROR, "유효하지 않은 UserDetail 타입입니다."),
14-
TOKEN_NOT_FOUND(HttpStatus.UNAUTHORIZED, "Authorization 헤더에 토큰이 없습니다."),
15-
EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 JWT 토큰입니다."),
16-
;
1710

18-
public static final String PREFIX = "[JWT ERROR]";
11+
INVALID_USER_ID_TYPE(HttpStatus.BAD_REQUEST, "사용자 ID의 타입이 유효하지 않습니다."),
12+
EMPTY_TOKEN(HttpStatus.BAD_REQUEST, "토큰이 비어있거나 유효하지 않은 형식입니다."),
1913

20-
private final HttpStatus status;
21-
private final String rawMessage;
14+
TOKEN_NOT_FOUND(HttpStatus.UNAUTHORIZED, "HTTP Authorization 헤더를 찾을 수 없습니다."),
15+
EXPIRED_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 토큰입니다."),
16+
MALFORMED_TOKEN(HttpStatus.UNAUTHORIZED, "잘못된 형식의 토큰입니다."),
17+
SIGNATURE_ERROR(HttpStatus.UNAUTHORIZED, "토큰 서명 검증에 실패했습니다."),
18+
UNSUPPORTED_TOKEN(HttpStatus.UNAUTHORIZED, "지원되지 않는 방식의 토큰입니다."),
19+
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다."),
20+
21+
UNEXPECTED_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "토큰 처리 중 예상치 못한 서버 오류가 발생했습니다.");
2222

23-
public String getMessage() {
24-
return PREFIX + " " + rawMessage;
25-
}
23+
private final HttpStatus status;
24+
private final String message;
2625
}

src/main/java/org/terning/terningserver/common/config/ValueConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.terning.terningserver.common.config;
22

3+
import jakarta.annotation.PostConstruct;
34
import lombok.Getter;
45
import org.springframework.beans.factory.annotation.Value;
56
import org.springframework.context.annotation.Configuration;
67

8+
import java.nio.charset.StandardCharsets;
9+
import java.util.Base64;
10+
711
@Configuration
812
@Getter
913
public class ValueConfig {
@@ -22,4 +26,9 @@ public class ValueConfig {
2226

2327
@Value("${jwt.refresh-token-expired}")
2428
private Long refreshTokenExpired;
29+
30+
@PostConstruct
31+
protected void init() {
32+
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes(StandardCharsets.UTF_8));
33+
}
2534
}

src/main/java/org/terning/terningserver/user/domain/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void updateProfile(String name, ProfileImage profileImage){
107107

108108
public void validateRefreshToken(String providedToken) {
109109
if (this.refreshToken == null || !this.refreshToken.equals(providedToken)) {
110-
throw new JwtException(JwtErrorCode.INVALID_JWT_TOKEN);
110+
throw new JwtException(JwtErrorCode.INVALID_TOKEN);
111111
}
112112
}
113113
}

0 commit comments

Comments
 (0)