/**
* token秘钥 统一使用的秘钥
*/
public static final String SECRET_UNIFORM = "XDD6897!&@#.,?";
/**
* token 过期时间: 3天
*/
public static final int calendarField = Calendar.DATE;
public static final int calendarInterval = 3;
Calendar nowTime = Calendar.getInstance();
nowTime.add(calendarField, calendarInterval);
Date expiresDate = nowTime.getTime();
Map<String, Object> map = new HashMap<>();
map.put("alg", "HS256");
map.put("typ", "JWT");
String tokenKey = tokenType.getType() + userId.toString();
String token = JWT.create().withHeader(map) // header
.withClaim("iss", "Service") // payload
.withClaim("aud", "APP")
.withClaim("userId", null == userId ? null : userId.toString())
.withClaim("type",tokenType.getType())
.withClaim("tokenKey",tokenKey)
.withArrayClaim("roles", roles)
.withIssuedAt(iatDate) // sign time
.withExpiresAt(expiresDate) // expire time
.sign(Algorithm.HMAC256(SECRET_UNIFORM)); //
springboot token生成方式
最新推荐文章于 2024-07-19 11:10:17 发布