前言:这里是将Springboot整合JWT测试篇(一)封装成工具类
public class JWTUtils { //秘钥自己保管好 private static String SECRET = "token!Q@W3e4r"; /** * 生成token * @param map //传入payload * @return 返回token */ public static String getToken(Map<String,String> map){ JWTCreator.Builder builder = JWT.create(); map.forEach((k,v)->{ builder.withClaim(k,v); }); Calendar instance = Calendar.getInstance(); instance.add(Calendar.DATE,3);//默认3天过期 builder.withExpiresAt(instance.getTime());//指定过期时间 return builder.sign(Algorithm.HMAC256(TOKEN)); } /** * 验证token * @param token * @return */ public static DecodedJWT verify(String token){ // 有任何验证异常,此处都会抛出 return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token); } /** * 获取token中payload数据 * @param token * @return */ public static Map<String, Claim> getPayloadFromToken(String token) { return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token).getClaims(); } }
标签:return,Springboot,JWT,token,类篇,static,public,String From: https://www.cnblogs.com/Life-QX/p/17140267.html