欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

区别jjwt生成的jwt token的不同版本

最编程 2024-01-13 18:29:52
...

第一个问题

The signing key’s algorithm ‘AES’ does not equal a valid HmacSHA* algorithm name and cannot be used with HS256

    // jjwt 0.9.0版本
    private static SecretKey generateKey(String secret) {
        byte[] encodedKey = Base64.decodeBase64(secret);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }
    // jjwt 0.11.2版本
    private static SecretKey generateKey(String secret) {
        byte[] encodedKey = Base64.decodeBase64(secret);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "HmacSHA256");
    }

AES改为HmacSHA256

第二个问题

The signing key’s size is 16 bits which is not secure enough for the HS256 algorithm.

// jjwt 0.9版本
String compactJws = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, generateKey("jinan_20220511"))
                .compact();
// jjwt 0.11.2版本
String compactJws = Jwts.builder()
                .setClaims(claims)
                .signWith(generateKey("jinan_20220511jinan_20220511jinan_20220511jinan_20220511"), SignatureAlgorithm.HS256)
                .serializeToJsonWith(new GsonSerializer<>(new Gson()))
                .compact();

密钥位数不够,必须大于256位,一个字符按照8位算,至少32个字符。

第三个问题

Unable to find an implementation for interface io.jsonwebtoken.io.Serializer using java.util.ServiceLoader.
代码参考第二个问题。

没找到序列化的实现,添加序列化相关依赖和代码。

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-gson</artifactId>
        <version>0.11.2</version>
    </dependency>
String compactJws = Jwts.builder()
                .setClaims(claims)
                .signWith(generateKey("jinan_20220511jinan_20220511jinan_20220511jinan_20220511"), SignatureAlgorithm.HS256)
                // 添加序列化相关
                .serializeToJsonWith(new GsonSerializer<>(new Gson()))
                .compact();

推荐阅读