利用hutool生成和验证JWT的示例
package com.sedaier.calcdir.test;
import java.util.HashMap;
import java.util.Map;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
public class JwtTest {
public static void main(String[] args) {
DateTime now = DateTime.now();
DateTime newTime = now.offsetNew(DateField.MINUTE, 10);
Map<String,Object> payload = new HashMap<String,Object>();
//签发时间
payload.put(JWTPayload.ISSUED_AT, now);
//过期时间
payload.put(JWTPayload.EXPIRES_AT, newTime);
//生效时间
payload.put(JWTPayload.NOT_BEFORE, now);
//载荷
payload.put("userName", "zhangsan");
payload.put("passWord", "666889");
String key = "aabb";
String token = JWTUtil.createToken(payload, key.getBytes());
System.out.println(token);
}
}