gpt4 book ai didi

rest - JWT 认证和刷新 token 实现

转载 作者:行者123 更新时间:2023-12-04 03:25:51 27 4
gpt4 key购买 nike

我正在开发一个具有自己的身份验证和授权机制的 REST 应用程序。我想使用 JSON Web Tokens 进行身份验证。以下是有效且安全的实现吗?

  • 将开发一个 REST API 来接受用户名和密码并进行身份验证。要使用的 HTTP 方法是 POST,因此没有缓存。此外,在传输时将有 SSL 安全性
  • 在身份验证时,将创建两个 JWT - 访问 token 和刷新 token 。刷新 token 将具有更长的有效期。两个 token 都将写入 cookie,以便在每个后续请求中发送它们
  • 在每次 REST API 调用中,将从 HTTP header 中检索 token 。如果访问 token 未过期,请检查用户的权限并相应地允许访问。如果访问 token 已过期但刷新 token 有效,则重新创建新的访问 token 并使用新的过期日期刷新 token (进行所有必要的检查以确保用户进行身份验证的权限未被撤销)并通过 Cookie
  • 发回
  • 提供一个注销 REST API,它将重置 cookie,因此后续 API 调用将被拒绝,直到登录完成。

  • 我对刷新 token 的理解是:

    由于刷新 token 的存在,我们可以为访问 token 保留较短的有效期,并经常检查(在访问 token 到期时)用户仍然被授权登录。

    如果我错了,请纠正我。

    最佳答案

    A REST API will be developed to accept username and password and do the authentication. The HTTP method to be used is POST so that there is no caching. Also, there will be SSL for security at the time of transit



    这是大多数人的做法,所以你在这里很好。

    At the time of authentication, two JWTs will be created - access token and refresh token. Refresh token will have longer validity. Both the tokens will be written in cookies so that they are sent in every subsequent requests



    将 token 存储在 cookie 中本身并不危险,但如果你以某种方式让你的服务器上的 JWT 模块从那里读取它们,你就容易受到 CSRF 攻击,其中任何网页都可以触发用户浏览器发送表单 + 你的站点 cookie 到你的服务器,除非您使用 CSRF token 。因此,通常它们存储在 localStorage 中,并且每次都“手动”添加到请求 header 中。

    On every REST API call, the tokens will be retrieved from the HTTP header. If the access token is not expired, check the privileges of the user and allow access accordingly. If the access token is expired but the refresh token is valid, recreate new access token and refresh token with new expiry dates (do all necessary checks to ensure that the user rights to authenticate are not revoked) and sent back through Cookies



    除了 cookies 的危险,它似乎是安全的。

    Provide a logout REST API that will reset the cookie and hence subsequent API calls will be rejected until login is done.



    您甚至不需要进行 API 调用,您只需清除 cookie 或 localStorage 对象并确保您的客户端不会因丢失 token 而中断。

    express-jwt 模块的标准要求 token 位于其自己的“授权:承载 [ token ]” header 中,我强烈建议使用 cookie。 localStorage API 一直可用到 IE8所以你应该很好。

    编辑:

    首先,了解 XSS 和 CSRF 攻击之间的区别很重要,因为它们通常被认为是同一件事。

    XSS 是指用户在其他用户的浏览器中在您的域上运行不安全的 JS,当这种情况发生时,localStorage 中的 JWT 或 session 中的 JWT 和 cookie 中的 JWT 都不安全。使用 cookie 上的 httpOnly 标志,您无法直接访问它们,但浏览器仍会将它们与 AJAX 请求一起发送到您的服务器。如果发生这种情况,你通常会倒霉。为防止这种情况,请确保在将所有用户输入发送到浏览器时对其进行转义。

    如果您使用脚本标签或 iframe 加载第 3 方 JS,除非您小心,否则这可能会损害 localStorage,但我在这方面的工作还不够,无法在这里为您提供帮助。

    只有当其他域试图通过让浏览器自动发送 cookie 来将普通 HTML 表单发送到您的服务器时,CSRF 才会出现。框架通过插入唯一的随机字符串作为隐藏字段并在提交时再次检查它们来防止这种情况。 localStorage 中的 JWT 是安全的,因为每个域都有自己独立的 localStorage 区域。

    但最终这一切都取决于您的服务是否将使用单个域,在这种情况下 httpOnly cookie 将非常安全且易于设置,但是如果您想将服务分散到多个域上,例如 api.domain.com + app .domain.com 或添加一个 native 应用程序,您被迫将 JWT 存储在 localStorage 或其他一些 native 存储区域。

    希望这可以帮助!

    关于rest - JWT 认证和刷新 token 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614259/

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