Generated with Java JJWT signature fails at jwt.io debugger(在 jwt.io 调试器中使用 Java JJWT 签名生成失败)
问题描述
我在 servlet 上使用 jjwt Java 库在服务器端生成 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 实现 类
It is due to .signWith(SignatureAlgorithm.HS256, "secret"). It is implemented by DefaultJwtBuilder class
public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) 
此方法假定您提供的是 base64 密钥,并且 secret 不是 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")));
                        这篇关于在 jwt.io 调试器中使用 Java JJWT 签名生成失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jwt.io 调试器中使用 Java JJWT 签名生成失败
				
        
 
            
        - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 
						
						
						
						
						