我正在制作一个使用 JWT token 连接到 Api 的 javascript 客户端。在服务器端没有问题,我可以创建 token 对其进行签名,然后验证签名,从而确保没有人篡改 token 。
但我如何在客户端执行此操作。我可以解码 JWT token 并查看 header 、有效负载和签名。但是我如何在客户端验证签名?是否有用于此的库,我如何将公钥传输给客户端?
如果我不验证签名,我怎么知道 token 没有被篡改?
最佳答案
if I do not validate the signature at the client side how can I ensure that the token is indeed from the server.? Maybe there is somebody in the middle who is changing the token
签名验证无法避免中间人攻击。攻击者甚至可以使用有效 token 嗅探 channel 以捕获凭据或更改消息
使用SSL/TLS channel (https)
If I do not verify the signature how can I know the token is not tampered with?
TLS 可信服务器提供的 token 可能有效。(它可能已在本地存储中被更改)。您可以验证签名。此操作通常在服务器端完成(请参阅@sakuto 回答),但您可以在浏览器中完美完成
But how do i verify the signature at the client site?
这些是步骤
- 从受信任的服务器下载公钥
- 从 JWT 中提取签名并对其进行解码(base64url)
- 使用加密库验证数字签名
我建议使用 Webcrypto。请在此处查看 RSA 导入 key 验证示例:https://github.com/diafygi/webcrypto-examples/blob/master/README.md#rsassa-pkcs1-v1_5
关于javascript - JWT token 签名验证 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47835736/