The signing key‘s size is 48 bits which is not secure enough for the HS256 algorithm

设置的私钥字符长度不满足要求, HMAC-SHA需要至少256bits的大小,一个字符是8bits,所以私钥至少需要32个字符。

其他可查询文档:jjwt-api 0.12.6 javadoc (io.jsonwebtoken)

附上Jwt版本以及测试代码:

<!--JWT令牌-->
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.12.5</version>
</dependency>
package com.hmdp;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;

@SpringBootTest
class HmDianPingApplicationTests {
    private final String secret = "123456123456123456123456123456123456";
    @Test
    public void genJwtPassword(){
        HashMap<String, String> studentAccount = new HashMap<>();
        studentAccount.put("account","2024");
        studentAccount.put("password","123456");
        // signWith:设置签名算法以及密钥,claims:设置自定义内容,expiration:设置令牌过期时间
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8)); // 通过密钥自动选择合适的算法,并返回对应的SecretKey对象
        String JwtPass = Jwts.builder().signWith(key)
                .claims(studentAccount).expiration(new Date(System.currentTimeMillis() + 3600 * 1000)).compact();
        System.out.println(JwtPass);
        GetJwt(JwtPass);

    }

    public void GetJwt(String Jwt) {
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));// 通过密钥自动选择合适的算法,并返回对应的SecretKey对象
        Claims payload = Jwts.parser().verifyWith(key).build().parseSignedClaims(Jwt).getPayload();
        System.out.println(payload);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值