gpt4 book ai didi

node.js - JWT 在 iOS Swift 应用程序中使用真的安全吗?

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:58 26 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它要求用户能够登录、注册帐户和注销。

在应用程序中,登录后,他们可以执行各种操作。但现在这(真的)不重要了。我使用 Node.js 作为后端,并且在凭据正确时发回 JSON Web token (JWT)。现在我只是想知道这是否真的安全..

上周我一直在互联网上寻找构建基于用户名和密码的身份验证系统的最安全方法。我使用 bcrypt 将密码存储在 MySQL 数据库中。匹配它们效果很好,所以我实际上可以登录 - 但我想生成一个 key 以允许随后访问,这样用户就不必在他访问的每个页面上登录。

JWT 似乎是一个不错的选择,但我读得越多,我就越相信它并不像我希望的那么安全。毕竟,我不希望有人检索 JSON Web token ,将其更改为其他内容,然后突然可以访问其他人的帐户。

下面显示了我如何使用 JWT 的示例:

const payload = {
userName: rows[0].userName
};

var token = jwt.sign(payload, secret, {
// expiresInMinutes: 1440
});

如果我尝试对其进行解码,我很快就可以检索到用户名。这不安全。

我对此进行了很多研究,有很多讨论,但我不明白为什么这么多应用程序使用它。我一定在安全性方面遗漏了一些东西,或者我如何才能使它成为一个更好的系统来使用。如果没有,我是否应该研究任何其他系统来使用用户名和密码对我的用户进行身份验证并保持登录状态?

最佳答案

使 JWT 安全的部分是签名。 JWT 包含三个部分:

  • 标题
  • 有效负载
  • 签名

以下引用自 https://jwt.io/introduction/ :

To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)

The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.

关于node.js - JWT 在 iOS Swift 应用程序中使用真的安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54244145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com