Express.js的Restful API身份验证和会话管理

11
  • Server side
  • The server must verify the token ID on each request to ensure that the user is authorized to access the requested resource. The easiest way is to decode the token and verify its signature.

    To summarize, I am researching how to implement token-based authentication for my Express v4 web application that will serve both mobile and web clients. I want to create a secure connection without using Basic Authentication or Passport middleware. When a new user registers on the client-side, the username and password are sent to the server, which then generates a token ID that is sent back to the client-side. The hashed password and salt are stored on the server-side. To store the token ID, I am considering using cookies, but I am unsure if this violates RESTful principles. Finally, when making requests to the server, the token ID is placed in the authorization header and verified by the server on each request.

    • 服务器端

    当收到请求时,服务器将检查令牌API,并将其与会话令牌进行比较,如果相同,则允许请求,否则拒绝

    这是Express应用程序授权的标准方式吗?

    很抱歉发了一篇冗长的帖子,但我觉得我确实应该掌握身份验证和授权,因为它很重要。我希望有人可以纠正我的REST身份验证误解并回答我的问题或建议更好的方法。


    我目前也在苦恼同样的问题 - 你解决了如何以流畅透明的方式实现它吗?有没有一种优雅的方法可以剥离所有请求的令牌,以便API只关注于提供所请求的数据?理想情况下,我们应该将身份验证和授权放在中间件中,在实际API之前执行。如果您能花时间解释您最终采用的解决方案,我会非常感激。 - Max Gordon
    2个回答

    1
    • 使用https编码发送用户凭据
    • 为了比较客户端的令牌,您可以将其保存在映射或Redis存储中,与用户id对应并进行匹配以考虑用户身份验证。这不会削弱Rest的重要性,因为在Rest中,授权令牌也仅是会话,过期后无效。
    • Express没有任何特定或标准的授权方法,它只允许您在后端使用任何db来执行所需的身份验证和授权。

    关于cookie,它必须在客户端使用以存储令牌ID吗?如果我使用cookie,那么我如何在移动应用程序中使用该cookie?我希望您能回答我上面提出的一些问题! - Tim
    这是一个非常好的问题...我想知道如何在需要通过API进行身份验证的本机应用程序中维护移动设备的会话的答案。我认为我要使用的方法是直接将信息存储在客户端给出的令牌中。我遵循了这个指南(http://thejackalofjavascript.com/architecting-a-restful-node-js-app/) 正如您所看到的,令牌是通过加密Javascript对象生成的,在其中保存了过期日期和一些用户信息。在这种情况下,也许不需要会话。 - Luca Marzi

    0

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接