api - 关于 JSON Web Token 安全性的几个问题?

标签 api security authentication jwt

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

3年前关闭。




Improve this question




我一直在研究使用 Tomcat API 发送/接收数据、登录、注销等的 Android/IOS 应用程序。我的主要身份验证形式是使用 SHA-512 通过带有 HMAC 的 JSON Web token 。身份验证照常执行。用户在第一次登录时提供其凭据(用户 ID 和密码)。服务器验证凭据,如果它们正确,则会生成 JWT 并将其返回给用户,用户可以使用该 JWT 在 future 的请求中对自己进行身份验证。 token 包含一个自定义声明,用于指定用户 ID,用于了解发出请求的用户。我做了一些关于 JWT 的研究,但在许多问题上意见不一。我有几个问题希望你能解释一下:

1- 使用 JWT 作为 API 的唯一身份验证机制是否足够?

2- 在安全性方面,使用 HMAC 的 JWT 和使用 RSA 的 JWT 之间有区别吗?

3- 理想情况下,我应该在哪里存储用于对 token 进行签名的对称签名 key ?目前,我将 key 直接传递给生成 token 的函数。这样做安全吗?

4- 我应该定期更改签名 key 以提高安全性吗?

5- 我可以相信我插入到 token 中的用户 ID 声明来识别发出请求的用户吗?

6- token 是否有“理想的”到期时间?有些人建议只要 15 分钟,而另一些人则说 3 小时就可以了。

7- 我是否应该担心限制特定用户可以拥有的代币数量?一个用户可能拥有多个移动设备,并且在任何时候都可能拥有多个 token 。在这种情况下,只要用户拥有凭据,就不会阻止用户从我的服务器获取数千个 token 。我应该实现某种机制(例如:数据库)来跟踪用户拥有的 token 吗?如果我在验证/生成 token 时必须进行额外的数据库查询,这似乎违背了 JWT 的目的并增加了复杂性。

8- 我是否需要担心撤销 token ?有些人认为拥有较短到期时间的 token 就足够了。其他人指出,如果用户退出应用程序后不撤销 token ,则永远不会有真正的注销机制。只是等待 token 过期是错误的吗?在安全方面,我什么时候需要撤销 token ?

抱歉,帖子太长了。我一直在担心处理这些问题的最佳方法。我很感激任何帮助。谢谢

最佳答案

1- Is it enough to use JWT as the sole authentication mechanism of my API?



通常是的,只要“签名 key ”具有足够的强度(128 位,由 CSPRNG 生成)。我将在下面的其他答案中介绍它是否“足够”。

2- In terms of security, is there a difference between JWT using HMAC and JWT using RSA?



通常使用 HMAC,因为它们的 key 长度要短得多,因为根本不需要允许客户端访问公钥,因为它们将通过您的 TLS/SSL 证书提供的公钥基础结构对您的服务器进行身份验证。

3- Ideally, where should I store the symmetric signing key that I am signing my tokens with? Currently, I am passing the key directly to the function that generates my tokens. Is it safe to do this?



您应该将其存储在无法通过 Web 读取的应用程序配置文件中。这使它在每个部署中都不同(例如,在您的预发布环境中提供的 token 不能用于您的生产环境中的身份验证)。

4- Should I change the signing key periodically for better security?



不,如果它具有足够的强度(128 位熵,如上所述),并且没有泄漏,则不会。

5- Can I trust the userID claim that I inserted into the token to identify the user making the request?



是的,这就是重点。如果您的 HMAC 是根据此声明计算的,则无法更改。

6- Is there an "ideal" expiration time for a token? Some people suggest as little as 15 mins while others say 3 hours is fine.



这取决于应用程序的“风险偏好”。例如,如果您的应用程序符合 PCI 规范,则该时间为 15 分钟。如果您对用户让他们的 session 保持更长时间打开感到高兴,那么这可以增加。

7- Should I worry about limiting the number of tokens that a particular user can have? A user may have multiple mobile devices and at any time may have multiple tokens. In this case, there is nothing stopping the user from obtaining thousands of tokens from my server provided that they have their credentials. Should I implement some mechanism (eg: database) to keep track of the tokens that a user has? This seems to defeat the purpose of JWTs and add complexity if I have to make additional database queries when verifying/generating a token.



不,正如您所说,这首先违背了 JWT 的目标。如果您允许通过 JWT 在客户端管理用户 session ,那么您不应该跟踪服务器端的 session 。由于 token 是客户端,因此您的应用程序应该不关心生成了多少,因为您没有额外的开销。如果您担心多个用户以同一个帐户登录,那么 JWT 不是可行的方法 - 服务器端管理系统会更好,其中颁发的 token 可以仅限于多个同时 session 。

8- Do I have to worry about revoking tokens? Some suggest that having tokens with a short expiration time is sufficient. Others point out that you can never have a true logout mechanism if you don't revoke tokens once the user logs out of the application. Is it wrong to just wait for the tokens to expire? In terms security, when would I ever need to revoke the token?



同样,这归结为“风险偏好”。如果密码被泄露,则更难撤销可能由攻击者创建的现有 session 。与注销链接类似,您所能做的就是响应客户端请求删除 cookie。

一种方法是在 JWT 中包含上次更改密码的日期和时间,这包含在 HMAC 计算中。然后这将在每个请求上进行检查,以确定它是否与您的数据库中的值匹配。如果没有,那么您将拒绝身份验证 token 并强制用户重新登录。这是您可以使用 JWT 进行撤销的一种方式。

注销更棘手,因为如果您在 token 中包含“注销日期/时间”,则一个注销请求将注销所有 session ,这是在服务器端检查的。同样,您的风险偏好将决定这是否值得实现,或者是否更容易设置一个较短的到期期限,以免攻击者检索的任何 token 可能继续无限期地更新(或者直到用户更改密码,如果您使用我之前的解决方案)。

关于api - 关于 JSON Web Token 安全性的几个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225244/

相关文章:

java - 有没有办法使用 JaCoCo Java API 来检测 jar 文件?

python - JSON 到 Python 对象

java - 如何为 Imgflip API 实现 post 方法

sql-server-2005 - 在 SQL 中拥有登录 ID 和 PersonID

javascript - 使用ajax登录时如何让浏览器提示保存密码

java - Spring RestTemplate URL 中的基本身份验证

api - Shopify:通过 API 调用从集合中删除产品

security - 数字签名 VS.信息摘要

mysql - 了解 aws 和 rds 连接

jsp - 跨站点历史操作解决方案