Java中 Jwt
2021-02-10 07:17
标签:expires subject image jwt ring info int code boolean jar包 Java中 Jwt 标签:expires subject image jwt ring info int code boolean 原文地址:https://www.cnblogs.com/tangshuo/p/12744277.htmljava
封装工具类
public class jwtTool {
public static final String secretKey = "askjdksadjkasdakjshdjkasdkAakjshdjasdjs";
public String getJWTWithHMAC256(String subject, String secretKey){
//指定JWT所使?用的签名算法
Algorithm algorithm = Algorithm.HMAC256(secretKey);
String token = JWT.create()//创建token
.withIssuer("com.kkb")//指定签发?人
.withSubject(subject)//指定主体数据
.withExpiresAt(new Date(new Date().getTime()+(1000*20)))
.sign(algorithm);//最终?生成 token
return token;
}
public boolean verifyTokenWithHMAC256(String token,String secretKey){
try{
Algorithm algorithm = Algorithm.HMAC256(secretKey);
JWTVerifier verifier = JWT.require(algorithm)
.withIssuer("com.kkb")
.build();
verifier.verify(token);
return true;
}catch (JWTVerificationException e){
e.printStackTrace();
return false;
}
}
}
DecodedJWT decode = JWT.decode(token);
System.out.println(decode.getSignature());
System.out.println(decode.getSubject());
System.out.println(decode.getExpiresAt());
下一篇:c++之io操作