11package org .terning .terningserver .auth .jwt ;
22
3- import io .jsonwebtoken .*;
3+ import io .jsonwebtoken .Claims ;
4+ import io .jsonwebtoken .ExpiredJwtException ;
5+ import io .jsonwebtoken .Jwts ;
6+ import io .jsonwebtoken .MalformedJwtException ;
7+ import io .jsonwebtoken .UnsupportedJwtException ;
48import io .jsonwebtoken .security .Keys ;
9+ import io .jsonwebtoken .security .SecurityException ;
510import jakarta .annotation .PostConstruct ;
611import lombok .RequiredArgsConstructor ;
712import org .springframework .stereotype .Component ;
813import org .terning .terningserver .auth .dto .Token ;
9- import org .terning .terningserver .common .config .ValueConfig ;
1014import org .terning .terningserver .auth .jwt .exception .JwtErrorCode ;
15+ import org .terning .terningserver .auth .jwt .exception .JwtException ;
16+ import org .terning .terningserver .common .config .ValueConfig ;
17+
1118
1219import javax .crypto .SecretKey ;
20+ import java .nio .charset .StandardCharsets ;
1321import java .util .Date ;
1422
1523@ Component
@@ -24,7 +32,7 @@ public class JwtProvider {
2432
2533 @ PostConstruct
2634 protected void init () {
27- secretKey = Keys .hmacShaKeyFor (valueConfig .getSecretKey ().getBytes ());
35+ secretKey = Keys .hmacShaKeyFor (valueConfig .getSecretKey ().getBytes (StandardCharsets . UTF_8 ));
2836 }
2937
3038 public Token generateTokens (Long userId ) {
@@ -47,14 +55,14 @@ public Long getUserIdFrom(String authorizationHeader) {
4755 if (userIdClaim instanceof Number ) {
4856 return ((Number ) userIdClaim ).longValue ();
4957 }
50- throw new JwtException (JwtErrorCode .INVALID_USER_ID_TYPE . getMessage () );
58+ throw new JwtException (JwtErrorCode .INVALID_USER_ID_TYPE );
5159 }
5260
5361 public String resolveToken (String rawToken ) {
5462 if (rawToken != null && rawToken .startsWith (TOKEN_PREFIX )) {
5563 return rawToken .substring (TOKEN_PREFIX .length ());
5664 }
57- throw new JwtException (JwtErrorCode .TOKEN_NOT_FOUND . getMessage () );
65+ throw new JwtException (JwtErrorCode .TOKEN_NOT_FOUND );
5866 }
5967
6068 private String generateToken (Long userId , long expiration ) {
@@ -77,9 +85,9 @@ private Claims parseClaims(String token) {
7785 .parseClaimsJws (token )
7886 .getBody ();
7987 } catch (ExpiredJwtException e ) {
80- throw new JwtException (JwtErrorCode .EXPIRED_JWT_TOKEN . getMessage () );
88+ throw new JwtException (JwtErrorCode .EXPIRED_JWT_TOKEN );
8189 } catch (UnsupportedJwtException | MalformedJwtException | SecurityException | IllegalArgumentException e ) {
82- throw new JwtException (JwtErrorCode .INVALID_JWT_TOKEN . getMessage () );
90+ throw new JwtException (JwtErrorCode .INVALID_JWT_TOKEN );
8391 }
8492 }
8593}
0 commit comments