JWT应用

JWT:Json Web Token是实际上是就是要服务器端给用户浏览器端返回一个token密钥值,防止任何一个用户都可以访问后台的接口内容

具体的内容入下:

header:标头通常由两部分组成: 令牌的类型(即JWT) 和所使⽤的签名算法,例如HMAC、SHA256或RSA。 它会使⽤Base64 编码组成JWT 结构的第⼀部分.

{ 2 "alg":"HS256", 3 "typ":"JWT" 4 }

2.Payload
令牌的第⼆部分是有效负载,其中包含声明。声明是有关实体(通常是⽤⼾)和其他数据的声明。同样 的,它会使⽤Base64 编码组成JWT结构的第⼆部分
3.Signature
header和payload都是结果Base64编码过的,中间⽤.隔开,第三部分就是前⾯两部分合起来做签 名,密钥绝对⾃⼰保管好,签名值同样做Base64编码拼接在JWT后⾯。(签名并编码)

java具体的实现过程
pom文件中引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--引⼊jwt-->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.10.3</version>
        </dependency>
        <!--引⼊mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!--引⼊mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!--引⼊druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

测试类

class JwtApplicationTests {

    /**
     * 这里面是令牌的获取
     */
    @Test
    void contextLoads() {
         Calendar instance = Calendar.getInstance();//设置过期时间
        instance.add(Calendar.SECOND,200);//设置20s后过期
        HashMap<String, Object> map = new HashMap<>();
         String token = JWT.create()
                .withHeader(map)  //header(可以不写)
                .withClaim("userId", 21) //payload
                .withClaim("username", "xiaohei")//payload
                .withExpiresAt(instance.getTime())//指定令牌的过期时间
                .sign(Algorithm.HMAC256("hsbiudshvodj"));//这是签名signature
        System.out.println(token);
    }
    //获取令牌里面的数据
    @Test
    void test(){
//        创建验证对象
//         JWTVerifier verification = JWT.require(Algorithm.HMAC256("hsbiudshvodj")).build();
//         DecodedJWT decodedJWT = verification.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NTEzMjQ3NTgsInVzZXJJZCI6MjEsInVzZXJuYW1lIjoieGlhb2hlaSJ9.NlN2Tsqj3mxP0tMOKf27Bue2sbRXvx5WmfGw2yUuNio");
//        System.out.println(decodedJWT.getClaim("userId").asInt());
//        System.out.println(decodedJWT.getClaim("username").asString());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值