使用Java生成JJWT签名在jwt.io调试器中失败 [英] Generated with Java JJWT signature fails at jwt.io debugger

查看:2178
本文介绍了使用Java生成JJWT签名在jwt.io调试器中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jjwt Java库在servlet上生成jwt的服务器端,直接来自jjwt GitHub页面的代码片段 https://github.com/jwtk/jjwt 生成并打印出此令牌。

I am using the jjwt Java library for server side generation of jwt in on servlets, the code snipper below straight from the jjwt GitHub page https://github.com/jwtk/jjwt generates and prints out this token.

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.XIKER3owR8BS3Krhsksg9INh9VBSejdn_qN-ONtPans



 String compactJws = Jwts.builder()
      .setSubject("Joe")
      .signWith(SignatureAlgorithm.HS256, "secret")
      .compact();
  PrintWriter out = response.getWriter();
  out.println(compactJws);

但是,当我尝试在jwt.io的调试器上验证此令牌时,它未通过签名检查。
检查和取消选中秘密base64编码都不起作用

However, when I try to verify this token on jwt.io's debugger, it fails the signature check. Both checking and unchecking secret base64 encoded didn't work

我错误地使用了这个库吗?

Am I using the library wrongly?

推荐答案

尝试使用 secr 并检查base64选项:)

Try with secr and check the base64 option :)

这是由于 .signWith(SignatureAlgorithm.HS256,secret )。它由 DefaultJwtBuilder实现 class

It is due to .signWith(SignatureAlgorithm.HS256, "secret"). It is implemented by DefaultJwtBuilder class

public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) 

此方法假设您在base64中提供密钥而 秘密不是base64 即可。当方法从 base64 解码为 byte [] 时, jjwt 使用的java转换器提供字符串 secr 的表示形式,与 jwt.io

This method assumes that you are providing a key in base64 and secret is not base64. When the method decodes from base64 to byte[] the java converter used by jjwt provides a representation of the string secr which is different to the JavaScript decoder used at jwt.io

您可以使用

System.out.println(
                javax.xml.bind.DatatypeConverter.printBase64Binary(
                        javax.xml.bind.DatatypeConverter.parseBase64Binary("secret")));

这篇关于使用Java生成JJWT签名在jwt.io调试器中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆