1616public class JwtUtil {
1717
1818 private final SecretKey secretKey ;
19-
19+ private static final String USER_ID_KEY = "userId" ;
20+ private static final String TOKEN_TYPE_KEY = "token" ;
21+
2022 public JwtUtil (@ Value ("${jwt.secret}" ) String secret ) {
2123 secretKey = new SecretKeySpec (secret .getBytes (StandardCharsets .UTF_8 ),
2224 Jwts .SIG .HS256 .key ().build ().getAlgorithm ());
@@ -29,9 +31,9 @@ public Long getUserId(String token) {
2931 .build ()
3032 .parseSignedClaims (token )
3133 .getPayload ()
32- .get ("userId" , Long .class );
34+ .get (USER_ID_KEY , Long .class );
3335 } catch (ExpiredJwtException ex ){
34- return ex .getClaims ().get ("userId" , Long .class );
36+ return ex .getClaims ().get (USER_ID_KEY , Long .class );
3537 }
3638 }
3739
@@ -48,16 +50,16 @@ public String getRole(String token) {
4850 }
4951 }
5052
51- public String getCategory (String token ) {
53+ public String getTokenType (String token ) {
5254 try {
5355 return Jwts .parser ()
5456 .verifyWith (secretKey )
5557 .build ()
5658 .parseSignedClaims (token )
5759 .getPayload ()
58- .get ("category" , String .class );
60+ .get (TOKEN_TYPE_KEY , String .class );
5961 }catch (ExpiredJwtException ex ){
60- return ex .getClaims ().get ("category" , String .class );
62+ return ex .getClaims ().get (TOKEN_TYPE_KEY , String .class );
6163 }
6264 }
6365
@@ -71,10 +73,10 @@ public Boolean isExpired(String token) {
7173 .before (new Date ());
7274 }
7375
74- public String createJwt (String category , Long userId , String role , Long expiredMs ) {
76+ public String createJwt (String tokenType , Long userId , String role , Long expiredMs ) {
7577 return Jwts .builder ()
76- .claim ("category" , category )
77- .claim ("userId" , userId )
78+ .claim (TOKEN_TYPE_KEY , tokenType )
79+ .claim (USER_ID_KEY , userId )
7880 .claim ("role" , role )
7981 .issuedAt (new Date (System .currentTimeMillis ()))
8082 .expiration (new Date (System .currentTimeMillis () + expiredMs *1000 ))
0 commit comments