|
|
@@ -2,10 +2,14 @@ package cn.seecoder.common.util;
|
|
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
+import io.jsonwebtoken.security.Keys;
|
|
|
import lombok.Data;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.Key;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
@@ -37,6 +41,15 @@ public class JwtTokenUtil {
|
|
|
|
|
|
private String secret;
|
|
|
|
|
|
+ private Key key;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
|
|
+ this.key = Keys.hmacShaKeyFor(keyBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public Boolean isTokenExpired(String token) {
|
|
|
try {
|
|
|
Claims claims = getClaimsFromToken(token);
|
|
|
@@ -50,7 +63,7 @@ public class JwtTokenUtil {
|
|
|
public Claims getClaimsFromToken(String token) {
|
|
|
Claims claims;
|
|
|
try {
|
|
|
- claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
|
|
+ claims = Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
|
|
|
} catch (Exception e) {
|
|
|
claims = null;
|
|
|
}
|