写一个Jwt工具类
导入如下pom.xml依赖
<!--fastjson依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.33</version></dependency><!--jwt依赖--><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.0</version></dependency>
写一个JwtUtil工具类
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;/*** JWT工具类* @author zhangsan*/
public class JwtUtil {/*** 有效期为 60 * 60 * 1000 一个小时*/public static final Long JWT_TTL = 60 * 60 *1000L;/*** 设置秘钥明文*/public static final String JWT_KEY = "sangeng";public static String getUUID(){return UUID.randomUUID().toString().replaceAll("-", "");}/*** 生成jtw* @param subject token中要存放的数据(json格式)* @return Jwt值*/public static String createJWT(String subject) {// 设置过期时间JwtBuilder builder = getJwtBuilder(subject, null, getUUID());return builder.compact();}/*** 生成jtw* @param subject token中要存放的数据(json格式)* @param ttlMillis token超时时间* @return Jwt值*/public static String createJWT(String subject, Long ttlMillis) {// 设置过期时间JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());return builder.compact();}private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;SecretKey secretKey = generalKey();long nowMillis = System.currentTimeMillis();Date now = new Date(nowMillis);if ( ttlMillis == null) {ttlMillis = JwtUtil.JWT_TTL;}long expMillis = nowMillis + ttlMillis;Date expDate = new Date(expMillis);return Jwts.builder()//唯一的ID.setId(uuid)// 主题 可以是JSON数据.setSubject(subject)// 签发者.setIssuer("sg")// 签发时间.setIssuedAt(now)// 使用HS256对称加密算法签名, 第二个参数为秘钥.signWith(signatureAlgorithm, secretKey).setExpiration(expDate);}/*** 创建token* @param id Id* @param subject 主题* @param ttlMillis 时间戳* @return Jwt值*/public static String createJWT(String id, String subject, Long ttlMillis) {// 设置过期时间JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);return builder.compact();}public static void main(String[] args) throws Exception {// 需要存放在token中的数据(可以是json格式的字符串)String subject = "{\"user\":\"tom\"}";// 生成一个有效期为1小时的JWTString token = JwtUtil.createJWT(subject, JwtUtil.JWT_TTL);System.out.println("token is " + token);// 解析对应的tokenClaims claim = parseJWT(token);System.out.println("claim is " + claim);}/*** 生成加密后的秘钥 secretKey* @return 生成加密后的密钥*/public static SecretKey generalKey() {byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");}/*** 解析** @param jwt string值* @return 返回解析的值*/public static Claims parseJWT(String jwt) throws Exception {SecretKey secretKey = generalKey();return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jwt).getBody();}}
运行结果,我们可以看到,可以生成一个token,也可以解析出来对应token里携带的内容
放到对应的Jwt在线解析,可以看到解码成右边所示