JWT入门

2022-11-15 15:32:33 浏览数 (1)

一、创建Maven项目

1、项目

项目类型:Maven

groupId:com.atguigu

artifactId:jwt

2、基本依赖

<dependencies>     <!-- JWT -->     <dependency>         <groupId>io.jsonwebtoken</groupId>         <artifactId>jjwt</artifactId>         <version>0.7.0</version>     </dependency>     <!--junit-->     <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>4.12</version>     </dependency> </dependencies>

二、测试JWT

1、生成token

代码语言:javascript复制
public class JwtTests {

    //过期时间,毫秒,24小时
    private static long tokenExpiration = 24*60*60*1000;
    //秘钥
    private static String tokenSignKey = "atguigu123";

    @Test
    public void testCreateToken(){
        String token = Jwts.builder()
                .setHeaderParam("typ", "JWT") //令牌类型
                .setHeaderParam("alg", "HS256") //签名算法

                .setSubject("guli-user") //令牌主题
                .setIssuer("atguigu")//签发者
                .setAudience("atguigu")//接收者
                .setIssuedAt(new Date())//签发时间
                .setExpiration(new Date(System.currentTimeMillis()   tokenExpiration)) //过期时间
                .setNotBefore(new Date(System.currentTimeMillis()   20*1000)) //20秒后可用
                .setId(UUID.randomUUID().toString())

                .claim("nickname", "Helen")
                .claim("avatar", "1.jpg")

                .signWith(SignatureAlgorithm.HS256, tokenSignKey)//签名哈希
                .compact(); //转换成字符串

        System.out.println(token);
    }
}

 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpc3MiOiJhdGd1aWd1IiwiYXVkIjoiYXRndWlndSIsImlhdCI6MTY2MjYzNTc0NiwiZXhwIjoxNjYyNzIyMTQ2LCJuYmYiOjE2NjI2MzU3NjYsImp0aSI6ImQ0OGE2MTY3LWNiNzMtNGQyMi1hMjg2LTJjNDU2YWYyOTY5MiIsIm5pY2tuYW1lIjoiSGVsZW4iLCJhdmF0YXIiOiIxLmpwZyJ9.KR9MIgf0Hykpa78GyxcEAcZHe8R_onDGpr7c3Ns3Mg8

 2、解析token

代码语言:javascript复制
  @Test
    public void testGetUserInfo(){
        String token = Jwts.builder()
                .setHeaderParam("typ", "JWT") //令牌类型
                .setHeaderParam("alg", "HS256") //签名算法

                .setSubject("guli-user") //令牌主题
                .setIssuer("atguigu")//签发者
                .setAudience("atguigu")//接收者
                .setIssuedAt(new Date())//签发时间
                .setExpiration(new Date(System.currentTimeMillis()   tokenExpiration)) //过期时间
                .setNotBefore(new Date(System.currentTimeMillis())) //20秒后可用
                .setId(UUID.randomUUID().toString())

                .claim("nickname", "Helen")
                .claim("avatar", "1.jpg")

                .signWith(SignatureAlgorithm.HS256, tockenSigKey)//签名哈希
                .compact(); //转换成字符串

        System.out.println(token);
        JwtParser parser = Jwts.parser();
        Jws<Claims> claimsJws = parser.setSigningKey(tockenSigKey).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        String subject = body.getSubject();
        Date issuedAt = body.getIssuedAt();
        Date notBefore = body.getNotBefore();
        String signature = claimsJws.getSignature();
        System.out.println(subject);
        System.out.println(notBefore);
        System.out.println(issuedAt);
        System.out.println(signature);
    }

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpc3MiOiJhdGd1aWd1IiwiYXVkIjoiYXRndWlndSIsImlhdCI6MTY2MjYzNTg4NywiZXhwIjoxNjYyNzIyMjg3LCJuYmYiOjE2NjI2MzU4ODcsImp0aSI6IjA0ZTBiZDk3LTFkOWItNGY5MS04M2RkLWEyMGI1OWI4Yjk1NiIsIm5pY2tuYW1lIjoiSGVsZW4iLCJhdmF0YXIiOiIxLmpwZyJ9.LPFZG5d5Y2YQuFjEKJQdrjrl_i9m5YsXECyK2qd232w guli-user Thu Sep 08 19:18:07 CST 2022 Thu Sep 08 19:18:07 CST 2022 LPFZG5d5Y2YQuFjEKJQdrjrl_i9m5YsXECyK2qd232w 

0 人点赞