gpt4 book ai didi

session - 我需要使用 JSON Web Token token 的 session 存储吗?为什么不只使用 cookie?

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

我还是不明白JWT的主要目的是什么。至于我,唯一的目的是:

  • 克服CSRF
  • 并确保更好的移动支持(因为在某些情况下移动应用程序不支持 cookie)。

还有一种说法是,使用 JWT,您不必担心服务器端的 session 存储。我不清楚。 JWT 怎么可能完全取代服务器端的 session 存储呢?这是否意味着我们将所有 session 数据放入 JWT,对其进行加密并在每次响应时将其发送给客户端?但如果是这样,是否意味着服务器发出的 token 会根据我们用于存储在 session 中的数据而改变?据我所知,唯一阻止我们以这种方式使用 cookie(在服务器端没有 session 存储)的是 cookie 文件的大小限制 - 只有 4kb


还有我们还需要使用SSL来防止 session 劫持吗?请告诉我我的理解是否正确,或者还有其他方面的问题。

最佳答案

我觉得关于JWT的传说太多了。要理解它的本质,还得回到它最初的定义。

根据其official网站:

JSON Web Token (JWT) is an open standard (RFC 7519) that defines acompact and self-contained way for securely transmitting informationbetween parties as a JSON object. This information can be verified andtrusted because it is digitally signed.

所以本质上,JWT 提供的只是一种传输数据的方式。不多也不少。由于涉及多方,格式必须标准化。一旦格式标准化,就可以创建库以促进其采用。

再次来自official网站:

When should you use JSON Web Tokens?

There are some scenarios where JSON Web Tokens are useful:

Authentication:

This is the typical scenario for using JWT, once the user is logged in, each subsequent request will include the JWT,allowing the user to access routes, services, and resources that arepermitted with that token. Single Sign On is a feature that widelyuses JWT nowadays, because of its small overhead and its ability to beeasily used among systems of different domains.

Information Exchange:

JSON Web Tokens are a good way of securely transmitting information between parties, because as they can besigned, for example using public/private key pairs, you can be surethat the senders are who they say they are. Additionally, as thesignature is calculated using the header and the payload, you can alsoverify that the content hasn't changed.

因此,身份验证只是 JWT 的可能用例之一。虽然这确实是 JWT 的典型用法。

在认证方面,可以使用JWT代替session+cookie的方式,因为它可以节省服务器存储session的内存。 但是 yield 有多大,要看用户量和你的具体场景。如果只有几个客户端并且没有跨域身份验证要求,我认为您不需要放弃良好的旧 session + cookie 方法。

最后但同样重要的是,Session 不仅仅用于身份验证。它实际上意味着 to place HTTP requests and responses within a larger context。 .鉴于 JWT 的大小限制,我不确定 JWT 是否可以为此目的替换 session 。恕我直言,身份验证恰好是 session 的用例之一,因为此类信息必须是特定于用户的。还有许多其他好的场景可以证明 session 的合理性,例如购物车。

关于session - 我需要使用 JSON Web Token token 的 session 存储吗?为什么不只使用 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921242/

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