1. 导入jar包

<dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.2.0</version>
 </dependency>

2. 生成token信息

//        过期时间
        Date date = new Date(System.currentTimeMillis() + 10000);
//        密码加密作为私钥
        Algorithm algorithm = Algorithm.HMAC256(password);
//        携带其他参数信息
        String sign = JWT.create()
                .withClaim("username", username)
                .withClaim("id", 111)
                .withExpiresAt(date)
                .sign(algorithm);

3. 获取token信息

JWT.decode()

4. 验证

// algorithm为私钥
JWTVerifier verify = JWT.require(algorithm)
                    .withClaim("username", "ddddddd")
                    .build();
            verify.verify(token);

 

java生成设置token过期 java生成token_System