在浏览器上验证JWT [英] Verify JWT on browser

查看:224
本文介绍了在浏览器上验证JWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关 JWT 的信息,我知道它分为三个部分,分别是headerpayloadsignature.

I have been reading about JWT and I understand it has three parts namely, header, payload and signature.

我将哈希算法保留在标头中,将基本信息保留在有效载荷中,例如.有效负载中的名称,年龄,角色,有效期等,然后将两者都进行base64编码,然后使用 标头中指定的算法以获得JWT

I keep the hashing algorithm used in headers, basic information in a payload eg. name, age , role, expiry etc in payload and then both of these are base64 encoded and then hashed using the algorithm specified in headers to obtain the JWT

  1. 我有一个前端,可以在其中使用usernamepassword登录.
  2. 登录请求转到服务器,该服务器对其进行身份验证并返回JWT.让我们假设使用的算法是HS256,这是一种对称密钥算法.
  3. 因此服务器将使用secret key来生成JWT.
  4. 作为登录请求响应的一部分,浏览器将具有JWT.
  5. 现在这个JWT可能会被篡改,因此在使用它之前,我应该验证JWT的真实性.
  6. 要验证,我需要密钥.
  1. I have a frontend where I can login using username and password.
  2. The login request goes to a server which authenticates it and returns a JWT. Lets suppose the algo used is HS256 which is a symmetric key algorithm.
  3. So the server will have the secret key using which the JWT will be generated.
  4. As part fo login request's response, the browser will have the JWT.
  5. Now this JWT could be tampered with on the way so before it is used, I should verify the authenticity of JWT.
  6. To verify, I need the secret key.

问题:

  1. 如何在前端获得此secret key?
  2. 有效负载可以保留有关用户的任何信息(不能保留任何敏感信息,例如密码).既然JWT可能会被篡改,那么在不验证前端JWT的情况下使用有效载荷信息就没有危险吗?

推荐答案

接收JWT的服务器就是验证令牌的服务器.如果有人修改了令牌,则验证将失败并且访问将被拒绝,因为(希望如此)服务器外部没有人知道该机密,因此无法创建有效的令牌.如果客户知道秘密,尤其是.在浏览器/js环境中以及使用对称哈希算法,这存在很大的安全风险,有人可以窃取秘密并使用有效签名创建新令牌.

The server which receives the JWT is the one who verifies the token. If someone modified the token, verification will fail and access will be denied, as (hopefully) no one outside the server knows the secret and therefore can't create a valid token. If the client knows the secret, esp. in browser/js envrinoments and with symetric hashing algorithmns, it's a big security risk and someone could steal the secret and create a new token with a valid signature.

这篇关于在浏览器上验证JWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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