package com.example.demo.util;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
/**
* jwt令牌
*
* @author Aiden
* @date 2022/11/21
*/
public class JwtToken {
/**
* 16位密钥
*/
private static String secret="abcdffdsgfergegt";
//生成token
public static String setToken(String username){
Date start = new Date();
//一小时有效时间
long currentTime = System.currentTimeMillis() + 60* 60 * 1000;
Date end = new Date(currentTime);
return Jwts.builder()
.setHeaderParam("type","JWT")
.setSubject(username)
.setIssuedAt(start)
.setExpiration(end)
.signWith(SignatureAlgorithm.HS512,secret)
.compact();
}
/**
* 获取token并解析
* claims:声明,断言,在payload里面的每一对键值对都是一对claim,
*
* @param token 令牌
* @return {@link Claims}
*/
public static Claims getToke(String token){
return Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
.getBody();
}
}