在非 100% AJAX 的 Ajax 应用程序中使用 HTTP 身份验证的最佳方法是什么

发布于 2025-01-02 11:04:09 字数 460 浏览 1 评论 0 原文

我有一个标准的 HTML 登录页面,我更愿意使用它,而不是浏览器提供的标准 HTTP 身份验证弹出窗口。今天,我使用会话cookie来跟踪登录后的会话,但我希望是无状态的并每次都通过HTTP身份验证。我正在访问的网络服务已经支持这一点,所以这只是浏览器的问题。

在 jQuery 中添加身份验证凭据很简单,但我不知道如何保留它们。如果您从登录页面(一个 jsp)转到主页(另一个 jsp),您显然不会保留登录页面中的用户名和密码字段。我知道如果您从弹出窗口输入 HTTP 身份验证凭据,某些浏览器会存储它们,但我不知道使用 XHRRequest 时是否会存储它们。如果是的话,浏览器之间是否有很大的一致性?

此外,用户还需要能够“注销”应用程序。如果浏览器存储了身份验证凭据,是否可以使用 JavaScript 清除它们。

我觉得我不能成为第一个尝试解决这个问题的人。是否有一些 jQuery 插件或者已经可以处理这个问题的东西?或者根本不可能做我想做的事?

I have a standard HTML login page, which I would much rather use than the standard HTTP authentication pop-up provided by browsers. Today, I am using session cookies to keep track of the session after logging in, but I'd like to be stateless and pass the HTTP authentication every time. The web services I am hitting already support this, so this is a browser-only issue.

Adding authentication credentials is trivial in jQuery, but I don't know how to keep them around. If you go from the login page (a jsp) to the Home page (another jsp) you clearly don't keep the username and password fields from the login page. I know some browsers will store your HTTP authentication credentials if you enter them from the pop-up, but I don't know if they get stored when using an XHRRequest. If they do, is there much consistency among browsers?

Also, the user needs to be able to "sign out" of the application, too. If the browser stores the authentication credentials, is there a way to clear them using JavaScript.

I feel like I can't be the first person to try to solve this. Is there some jQuery plugin or something that already handles this? Or is it simply not possible to do what I'm trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧梦荧光笔 2025-01-09 11:04:09

您有 2 个选择:

1) 客户端存储凭据——这不是一个好主意。出于明显的原因,您不想在客户端上存储用户名/密码。如果您有密码的哈希版本,它可能不会那么糟糕,但仍然不推荐。无论如何,如果您要在客户端存储,则必须使用 cookie,或者 HTML5 本地存储(尚未得到广泛支持)

2) 服务器端凭证存储——通常通过会话完成。 将生成的会话 ID 传回客户端,并保存在 cookie 中或保存在每个后续 AJAX 调用的 URL 中(例如 ?SESSID=xyz)。

然后,可以 最安全、最可靠、最容易实施

You have 2 options:

1) Client-side storage of credentials -- not a good idea. For obvious reasons you don't want to store the username/password on the client. If you had a hashed version of the password, it might not be so bad, but still not recommended. In any case, if you're going to store on the client side, you either have to use a cookie, or HTML5 local storage (which is not widely supported, yet)

2) Server-side storage of credentials -- typically done with sessions. Then the resultant Session ID can be passed back to the client and persisted in either a cookie or in the URL of each subsequent AJAX call (?SESSID=xyz for example)

The server-side approach would be the most secure, reliable, and easiest to implement

酒浓于脸红 2025-01-09 11:04:09

好的,我会尽力帮助...

首先,了解 HTTP 身份验证的工作原理。有两个版本 - 基本版和摘要版。基本以明文形式传输,摘要以加密方式传输。通过这些类型的身份验证,用户名/密码将在每个请求的 HTTP 标头中传递。浏览器在登录时捕获这些信息,并将它们存储在无法访问的浏览器会话 cookie 中,该 cookie 在浏览器会话关闭时会被删除。因此,在回答您的问题之一时,您无法从 javascript 访问这些内容。

您可以为用户名和密码创建自己的会话 cookie 变量。用于此目的的 jQuery 函数非常简单。请参阅 jquery-cookie 模块作为如何设置会话 cookie 的示例。这些可以从会话 cookie 中检索并与每个 ajax 请求一起发送并在服务器中进行验证。然而,这并不是一种特别好的身份验证方法,因为嗅探网络将允许任何人轻松获取您的身份验证详细信息。但是,它会起作用。

使用基于会话 cookie 的身份验证(其中会话 ID 随每个请求一起发送)是实现此目的的最佳方法。在服务器端,您需要为每个 HTTP 请求调用一个函数。该函数应执行以下操作:

   check to see if the session has been authenticated
   if no:
       redirect to login screen
   if yes:
       do authorization and allow the user access to the page

大多数 Web 框架支持会话 cookie 身份验证和服务器上会话 ID 的管理。这绝对是要走的路。

Okay, I'll take a stab at helping ...

Firstly, understand how HTTP authentication works. There are two versions - Basic and Digest. Basic transmits in plaintext, digest is encrypted. With these types of authentication, the username/password are passed in the HTTP header with every single request. The browser captures these at login and they are stored in an inaccessible browser session cookie which is deleted when the browser session is closed. So, in answer to one of your questions, you can't access these from javascript.

You could create your own session cookie variables for username and password. The jQuery functions for this are really simple. See jquery-cookie module as one example of how to set session cookies. These could be retrieved from the session cookie and sent with each ajax request and validated in the server. However, this is not a particulary good way to do authentication since sniffing the network will allow anybody to easily grab your auth details. But, it would work.

Using session cookie based authentication where the session ID is sent sent with each request is the best way to do this. At the server side, you need to have a function called for every HTTP request. This function should do the following:

   check to see if the session has been authenticated
   if no:
       redirect to login screen
   if yes:
       do authorization and allow the user access to the page

Most web frameworks support session cookie authentication and the management of session ids at the server. This is definately the way to go.

瑕疵 2025-01-09 11:04:09

这很有趣。

使用 cookie 管理服务器上的用户会话。当用户第一次访问登录页面时创建一个会话,并通过响应将会话 ID/密钥作为值传递给其中一个 cookie。当用户通过身份验证时,将用户“密钥”信息放入 cookie 中,将“值”放入服务器的应用程序上下文中。用户登录后,任何后续请求都将根据服务器上的会话 cookie 值进行身份验证。授权将根据作为 cookie 值传递的用户“密钥”来完成。

注销时,从服务器清除基于会话的 cookie,并将站点刷新到默认页面。

Cookie 对于不同的浏览器来说很奇怪 - 只是一个注释;)

希望这会有所帮助。

This is interesting one.

Manage user sessions on server by use of cookies. Create a session when user first accesses the login page and pass the session id/key as value to one of the cookie via response. When the user is authenticated put user "key" info in cookie and "values" in application context at server. Once user is logged, any subsequent request will be authenticated based on session cookie value at server. Authorization will be done based on user "key" passed as cookie value.

On logout clear the session based cookies from server and refresh the site to default page.

Cookies are bizarre with different browsers - just a note ;)

Hope this helps.

只有一腔孤勇 2025-01-09 11:04:09

更新

下面的答案是在 2012 年发布的,链接大多已失效。然而,从那时起,使用 JSON Web 令牌< /a>.这是一篇好博客文章,解释了如何使用它们。


大多数答案都没有抓住重点,即避免进行任何服务器端会话。我不希望服务器中有任何应用程序状态。我将奖励最接近的答案,但真正的功劳归于 rest -讨论组乔恩Moore 提供正确答案,并感谢 Mike Amundsen 帮助我真正理解它。

我得到的最佳答案是使用 cookie,但不是大多数应用程序服务器提供给您的典型自动会话 id cookie。 cookie(将自动随每个后续请求发送)是由服务器签名的用户标识符和时间。您可以在 cookie 中包含过期时间,以便它模拟服务器上典型的 30 分钟会话(这意味着您必须通过后续请求将其向前推进),并防止相同的 cookie 永远有效。

XHR/AJAX 部分是转移注意力的部分。无论您是执行 XHR 请求还是老式的逐页 Web 应用程序,这都适用。要点是:

  • cookie 在后续请求中自动发送,因此没有
    需要特殊的脚本 - 这就是浏览器已经工作的方式。
  • 服务器不需要为用户存储任何session,因此用户
    可以访问集群中的任何服务器,而无需重新进行身份验证。

Update

The answer below was posted in 2012 and the links are mostly dead. However, since then, a more elegant standards-based approach to the same solution appeared using JSON Web Tokens. Here is a good blog post explaining how to use them.


Most answers miss the point, which is to avoid having any server-side session. I don't want any application state in the server. I'll award the bounty to answer that came closest, but the real credit goes to the rest-discuss group and Jon Moore for the correct answer and to Mike Amundsen for helping me to actually understand it.

The best answer I've gotten is to use a cookie, but not the typical automatic session id cookie given to you by most application servers. The cookie (which will automatically be sent with each subsequent request) is a user identifier and time signed by the server. You can include an expiration time with the cookie so it simulates the typical 30 minute session on a server (which means you have to push it forward with subsequent requests) as well as keeps the same cookie from being valid forever.

The XHR/AJAX part is a red herring. This will work whether you are doing XHR requests or an old-fashioned page-by-page web application. The main points are:

  • The cookie is automatically sent on subsequent requests so there's no
    special scripting required - it's just how browsers work already.
  • The server does not need to store any session for the user, so the user
    can hit any server in a cluster and not have to re-authenticate.
假面具 2025-01-09 11:04:09

有点有趣的是,您考虑将一些真实信息推送给客户端。如果您想要一个传统的解决方案,KOGI 的服务器端建议是您的最佳选择。

但您似乎也在询问有关涉及用户提供的秘密的内存泄漏的问题。好问题。但要总体回答这个问题,我想说它必须是特定于浏览器的。它是浏览器内部结构、javascript 引擎内部结构相关的,客户端应用程序(即浏览器或浏览器中的 js)存储用户输入的值。

这些值很可能不会在整个内存中不必要地复制,但无法保证这一点。除了负责任的 JavaScript 编码实践之外,您无法采取任何措施来保证用户输入位置的限制。

稍微离题

基本点是,如果您将其存储在客户端上,那么它并不是真正安全的 - 除非服务器使用只有服务器(或用户通过他们的密钥)才能在客户端上存储加密信息。正确的凭据),有。因此,您可以想象编写一个 JS 应用程序来在客户端上进行一些身份验证 - 与银行卡(过去?)通过检查卡上的 PIN 码而不是返回数据库来进行 POS 身份验证的方式非常相似。它基于(有点脆弱)假设,即用户无法直接读取/写入客户端上的“暗区”cookie/本地存储/银行卡上的磁条。因此,我仅建议将此作为虚假身份验证者的取消资格,而不是作为凭证的唯一限定符。

要点

如果您确实想要无状态,只需将用户凭据存储在本地存储中,或者作为 cookie 但使用服务器密钥对其进行加密。当您需要它们时,通过 HTTPS 将带有加密/使用存储凭据的 XHR 发送到服务器,让您的服务器解密它们并将它们发送到回调。然后传递这些 HTTPS 的明文来进行您的身份验证。

Slightly interesting in that you consider pushing some of the authent to the client. If you want a conventional solution, KOGI's server-side suggestion is the way to go.

But you also seem to be asking questions about memory leaks involving your user supplied secrets. Good questions. But to take a general stab at answering that I'd say it would have to be browser specific. It's browser internals, javascript engine internals -dependent where a client side application (i.e., the browser, or js in the browser) is storing the values the user inputs.

Most likely those values are not replicated needlessly throughout memory, but there's no way to guarantee that. Apart from responsible javascript coding practices, there's nothing you can do to guarantee the limit of locations of user inputs.

Slight digression

The basic point is if you store it on the client it is not really secure -- unless, the serve stores encrypted information on the client with a key that only the server (or the user via their correct credentials), has. So you could conceivably code a JS application to do some authent on the client -- much the same as how bank card (used to?) do POS authent by checking the PIN to the PIN on the card, and not back at the DB. It's based on the (somewhat flimsy) assumption the user has no direct read/write access of the "dark area" cookie/local storage on client / mag strip on bank card. So I would only advise this as disqualifier for false authents and not as a sole qualifier for the credentials.

Main point

If you do want to be stateless, just store user credentials in localstorage, or as a cookie but encrypt them with a server key. When you need them send an XHR with the encrypted / use stored credentials to the server over HTTPS, let your server decrypt them and send them to the callback. Then pass those cleartext of HTTPS to do your authent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文