1

我对Web浏览器的基本身份验证有点困惑。我曾经认为Web浏览器在收到前一个响应中的HTTP 401状态后才会发送Authorization标头。不过,Chrome看起来会随后向每个请求发送授权标头。它具有我从我的网站上为401响应而输入的数据,并随每条消息一起发送(根据Chrome和我的网络服务器附带的开发人员工具)。那是预期的行为?是否有一些标题我应该用我的401来推断授权的东西不应该被缓存?我目前正在使用WWW-Authenticate标头。了解基本身份验证与401

回答

7

这是为RFC 2617 (Section 2)定义浏览器的预期行为:

A client SHOULD assume that all paths at or deeper than the depth of 
the last symbolic element in the path field of the Request-URI also 
are within the protection space specified by the Basic realm value of 
the current challenge. A client MAY preemptively send the 
corresponding Authorization header with requests for resources in 
that space without receipt of another challenge from the server. 
Similarly, when a client sends a request to a proxy, it may reuse a 
userid and password in the Proxy-Authorization header field without 
receiving another challenge from the proxy server. See section 4 for 
security considerations associated with Basic authentication. 

据我所知,基本HTTP认证没有进行注销/重新认证的能力。这同时缺乏HTTP基本认证的安全性,这就是为什么大多数网站现在都使用表单和Cookie进行认证的解决方案。

+0

你是对的。 '注销'在基本身份验证的范围内是没有意义的,因为安全令牌在每个请求*中发送*。实质上,每个HTTP请求都是单独验证的。您可以强制浏览器停止身份验证,例如,请参阅http://stackoverflow.com/questions/5957822/how-to-clear-basic-authentication-details-in-chrome。 –

1

RFC 2617

如果事先请求已被授权,则 相同的凭证可以是用于该 保护空间内的所有其它请求一段由 认证方案,参数确定的时间重新使用和/或用户偏好。

从我的经验看,浏览器会自动发送基本凭据以供后续请求使用。它可以防止为额外的资源做额外的往返。