Commit 277608a6 by 袁伟铭

1.0.0

parent ff28e7d8
...@@ -85,19 +85,23 @@ public class ApiTokenUtils implements InitializingBean { ...@@ -85,19 +85,23 @@ public class ApiTokenUtils implements InitializingBean {
} }
} }
public static boolean isTokenValid(String token) { public static Claims getClaims(String token) {
try { try {
//解析JWT字符串中的数据,并进行最基础的验证 //解析JWT字符串中的数据,并进行最基础的验证
Claims claims = Jwts.parser() Claims claims = Jwts.parser()
.setSigningKey(DatatypeConverter.parseBase64Binary(properties.getBase64Secret())) .setSigningKey(DatatypeConverter.parseBase64Binary(properties.getBase64Secret()))
.parseClaimsJws(token) .parseClaimsJws(token)
.getBody(); .getBody();
return true; return claims;
} }
//在解析JWT字符串时,如果密钥不正确,将会解析失败,抛出SignatureException异常,说明该JWT字符串是伪造的 //在解析JWT字符串时,如果密钥不正确,将会解析失败,抛出SignatureException异常,说明该JWT字符串是伪造的
//在解析JWT字符串时,如果‘过期时间字段’已经早于当前时间,将会抛出ExpiredJwtException异常,说明本次请求已经失效 //在解析JWT字符串时,如果‘过期时间字段’已经早于当前时间,将会抛出ExpiredJwtException异常,说明本次请求已经失效
catch (SignatureException | ExpiredJwtException e) { catch (SignatureException | ExpiredJwtException e) {
return false; return null;
}
} }
public static boolean isTokenValid(String token) {
return getClaims(token) != null;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment