ajax 请求需要 401 授权,但重试成功
我们在服务器上使用 apache 和 NTLM:
NTLMAuth on
AuthType NTLM
AuthName "Auth"
NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
NTLMBasicAuthoritative on
require valid-user
当我们使用 ajax 时,这会出现错误,大多数 ajax 请求在第一次尝试时失败,并显示消息“401需要授权”,但在自动重试时它们会成功,为什么之间有区别两个请求?
我们的应用程序可以工作,但这个错误很烦人,占用资源并减慢应用程序的速度。
We use apache with NTLM on our server:
NTLMAuth on
AuthType NTLM
AuthName "Auth"
NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
NTLMBasicAuthoritative on
require valid-user
And this gives as an error when we use ajax, most of our ajax requests fail on the first try with message "401 Authorization Required" but on automatic retry they succeed, why is there a difference between both requests?
Our application works but this error is annoying, uses resources and slows down application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您实际看到的不是某种“自动重试”行为,而是挑战-NTLM的响应认证。就其性质而言,您必须为连接上的第一个请求添加额外的往返。身份验证应该如下所示:
所以是的,您将使用一些额外的资源来进行 NTLM 身份验证(无论如何,使用基本身份验证。)但请注意,NTLM 验证连接而不是请求。因此,通过同一保持活动 HTTP 连接的后续请求不需要重新进行身份验证。这减轻了挑战-响应性质的负担。
最后,请注意,大多数支持 NTLM 或 SPNEGO 的 Web 浏览器都为此做好了准备,并将使用 Expect/Continue。因此,它们不会在初始连接中发送 POST 数据(例如),直到它们通过身份验证并从服务器获得 HTTP 继续,这也应该减轻负担。
I suspect what you're actually seeing is not some sort of "automatic retry" behavior, but is the challenge-response authentication of NTLM. By its nature, you will have to add an extra round-trip to the first request on a connection. The authentication should look like this:
So yes, you will use some additional resources to do NTLM authentication (over using Basic authentication, anyway.) Note, however, that NTLM authenticates a connection not a request. Thus, subsequent requests over the same kept-alive HTTP connection do not need to reauthenticate. This lessens the burden of the challenge-response nature.
Finally, note that most web browsers that support NTLM or SPNEGO are prepared for this, and will use expect/continue. Thus, they will not send POST data (for example) in the initial connection until they're authenticated and get an HTTP continue from the server which should also lessen the burdern.