生成使用token
引入pom
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.14.0</version>
</dependency>
添加工具类
package com.mashibing.interinalcommon.util;标签:java,代码,auth0,公用,util,token,import,com From: https://www.cnblogs.com/188221creat/p/17884094.html
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
public class JwtUtil {
// 盐
private static final String SIGN = "CPE!@#";
public static String genertorToken(Map<String,String> map){
// 设置 token 过期时间 1 天
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,1);
Date date = calendar.getTime();
JWTCreator.Builder builder = JWT.create();
// 将 token 进行遍历 方到 map 中
map.forEach(
(k,v) ->{
builder.withClaim(k,v);
}
);
// 整合 过期时间
builder.withExpiresAt(date);
// 生成 token
String sign = builder.sign(Algorithm.HMAC256(SIGN));
return sign;
}
}