public class JwtTest {
public static void main(String[] args) throws JOSEException {
//生成jwt
//1.第一部分,头部,主要放一些常量,加密方式
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.HS256)
.type(JOSEObjectType.JWT).build();
System.out.println(jwsHeader.toBase64URL());
//2.第二部分,载荷
Map map = new HashMap();
map.put("userId", 1);
map.put("name", "zzw");
Payload payload = new Payload(map);
System.out.println(payload.toBase64URL());
//3.第三部分,签名 签名 = 头部 + 载荷 + 密钥
JWSObject jwsObject = new JWSObject(jwsHeader,payload);
//密钥
String key ="qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm";
MACSigner macSigner = new MACSigner(key);
//通过密钥加密
jwsObject.sign(macSigner);
String jwt = jwsObject.serialize();
System.out.println(jwt);
}
}
03-27
214
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-14
1477
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-10
111
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-10
1323
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22
2571
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)