严智鑫
2024-04-11 6763c264ab45bd4c3f688b0f6041b334f3bb0727
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mes.tools;
 
import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
 
import java.util.Date;
 
public class TokenTools {
 
    /**
     * 生成token
     * @return
     */
    public  static String getToken(String userid,String sign){
        return JWT.create().withAudience(userid) // 将 user id 保存到 token 里面,作为载荷
                .withExpiresAt(DateUtil.offsetHour(new Date(), 24)) // 2小时后token过期
                .sign(Algorithm.HMAC256(sign)); // 以 password 作为 token 的密钥
 
    }
}