問題描述
我在 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 簽名生成失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!