package com.mulun;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import java.util.Base64;
/**
* JWT 原理
*
* @author mulun
* @version 1.0
* @date 2021/6/15 08:18
*/
public class JWTDemo {
public static void main(String[] args) {
// 头部
JSONObject header = new JSONObject();
// 加密方式
header.put("Alg", "HS256");
// payload -Data
JSONObject payLoad = new JSONObject();
// 数据主体
payLoad.put("userName", "mulun99");
payLoad.put("userAge", "21");
payLoad.put("userId", "18888");
// Base64 编码
String jwtHeader = Base64.getEncoder().encodeToString(header.toJSONString(0).getBytes());
String jwtPayLoad = Base64.getEncoder().encodeToString(payLoad.toJSONString(0).getBytes());
// 盐值
String signingKey = "myKey";
// 验证签名
String sign = SecureUtil.md5(payLoad.toJSONString(0) + signingKey);
// 组装JWT
String jwt = jwtHeader + "." + jwtPayLoad + "." + sign;
System.out.println(jwt);
}
}
JWT原理
最新推荐文章于 2024-06-18 20:51:41 发布