gpt4 book ai didi

api - 使用 JWT 处理用户数据的最佳实践

转载 作者:行者123 更新时间:2023-12-04 04:33:58 26 4
gpt4 key购买 nike

我正在通过 Json Web Tokens (JWT) 实现无状态 REST API。目前,我想知道将用户数据传递到前端的最佳方法是什么。这些是我需要在前端访问的字段 username, email, role, full_name, description, profile_img, facebook_id, twitter_id, custom_setting_1, custom_setting_2, custom_setting_3, custom_setting_4
我看到有两个选项:

  • 在 JWT 创建期间,将用户数据添加到 JWT 负载中。然后在前端解码。虽然我担心如果我添加所有数据,有效载荷会变得非常大。
  • 我只能添加不可更改的字段,例如 username, role到智威汤逊。创建 JWT 并返回到前端后,我从 API 发送另一个对用户数据的请求。

  • 我也可能在这里遗漏了一些东西。所以想知道使用 JWT 处理用户数据的最佳方法是什么。

    最佳答案

    一旦您使用 JWT 进行身份验证(我知道您的服务器正在生成客户端需要在每个请求中发送到服务器的身份验证 token ),就没有必要在 token 中包含所有这些详细信息。

    你的第二种方法更有意义:

    I can add only unchangeable fields like username, role to JWT. After the JWT is created and returned to the front-end, I send another request for the user data from the API.



    保持您的 JWT 精简并执行另一个请求以获取用户详细信息。

    有关如何设计 URL 以返回经过身份验证的用户详细信息的更多信息,请查看以下内容:
  • How to design URL to return data from the current user in a REST API?
  • Is using magic (me/self) resource identifiers going against REST principles?
  • 关于api - 使用 JWT 处理用户数据的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37944556/

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