依赖:
com.auth0 java-jwt3.18.2
工具类
public class JwtUtils { public String createToken(Map user){ //设置算法以及签名 Algorithm algorithmHS = Algorithm.HMAC256("secret"); //设置头部 Map map = new HashMap<>(); map.put("typ","JWT"); map.put("alg","HS256"); String token = JWT.create().withHeader(map) //签发人 .withIssuer("auth0") //主题 .withSubject("login") //受众 .withAudience("users") //自定义载荷 .withClaim("name",user.get("name").toString()) .sign(algorithmHS); return token; } public Map verfiyToken(String token){ Map map = new HashMap(); Algorithm algorithmHS = Algorithm.HMAC256("secret"); JWTVerifier verifier = JWT.require(algorithmHS) .withIssuer("auth0") .build(); //Reusable verifier instance try { DecodedJWT jwt = verifier.verify(token); map.put("status",true); map.put("name",jwt.getClaim("name").asString()); return map; }catch (Exception e){ map.put("status",false); return map; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)