XiaoRuby
2023-07-05 23603498db6bc186eb7fe1e2757f77dd5419236c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.yuanchu.limslaboratory.utils;
 
 
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
 
import java.util.Date;
 
public class JwtUtils {
    // 过期时间 2 小时
//    private static final long EXPIRE_TIME = 2 * 60 * 60 * 1000;
 
    private static final long EXPIRE_TIME = 10;
    /**
     * 生成签名,准确地说是生成token
     * @param secret
     * @return
     */
    public static String sign(String account, String secret){
        try{
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            Algorithm algorithm = Algorithm.HMAC256(secret);
            //附带username,nickname信息
            return JWT.create()
                    .withClaim("account",account)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (JWTCreationException e){
            e.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}