Skip to content

Commit 3df89c2

Browse files
authored
Merge pull request #182 from CleanEngine/feat/login-parserfix
fix: reuse jwtparser in JWTUtil
2 parents d654203 + 644038b commit 3df89c2

File tree

1 file changed

+6
-2
lines changed
  • src/main/java/com/cleanengine/coin/user/login/application

1 file changed

+6
-2
lines changed

src/main/java/com/cleanengine/coin/user/login/application/JWTUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cleanengine.coin.user.login.application;
22

33

4+
import io.jsonwebtoken.JwtParser;
45
import io.jsonwebtoken.Jwts;
56
import org.springframework.beans.factory.annotation.Value;
67
import org.springframework.stereotype.Component;
@@ -15,19 +16,22 @@ public class JWTUtil {
1516

1617
private final SecretKey secretKey;
1718

19+
private final JwtParser jwtParser;
20+
1821
public JWTUtil(@Value("${spring.jwt.secret}") String secret) {
1922
secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), Jwts.SIG.HS256.key().build().getAlgorithm());
23+
jwtParser = Jwts.parser().verifyWith(secretKey).build();
2024
}
2125

2226
public Integer getUserId(String token) {
2327

24-
return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("userId", Integer.class);
28+
return jwtParser.parseSignedClaims(token).getPayload().get("userId", Integer.class);
2529

2630
}
2731

2832
public Boolean isExpired(String token) {
2933

30-
return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().getExpiration().before(new Date());
34+
return jwtParser.parseSignedClaims(token).getPayload().getExpiration().before(new Date());
3135
}
3236

3337
public String createJwt(Integer userId, Long expiredMs) {

0 commit comments

Comments
 (0)