Java使用auth0生成token信息


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);

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM