缺少身份验证的 HTTP 状态代码

发布于 2024-10-04 17:25:08 字数 178 浏览 0 评论 0原文

HTTP 为缺少身份验证定义了状态 401 Unauthorized,但此状态仅适用于HTTP 身份验证。当发生未经授权的请求时,我应该使用基于会话 cookie 的系统返回什么状态?

HTTP defines the status 401 Unauthorized for missing authentication, but this status only applies to HTTP authentication. What status should I return with a session cookie based system, when an unauthorized request happens?

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

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

发布评论

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

评论(2

失退 2024-10-11 17:25:08

从形式上来说,403 Forbidden 是正确的响应。它定义为

授权不会有任何帮助,并且不应重复请求。

令人困惑的部分可能是“授权不会有帮助”,但它们的真正意思是“HTTP 身份验证”(WWW-Authenticate)

Formally, 403 Forbidden is the right response. It's defined as

Authorization will not help and the request SHOULD NOT be repeated.

The confusing part may be "Authorization will not help", but they really mean "HTTP authentication" (WWW-Authenticate)

雨的味道风的声音 2024-10-11 17:25:08

403 我相信在技术上是正确的(如果您正在实现自定义 API/协议,这可能是最有效的)。

401 是不合适的,因为它指的是使用 WWW-Authenticate 标头进行授权,而会话 cookie 则不是。

如果这是一个面向公众的网站,您尝试根据会话 cookie 拒绝访问,则最好使用 200 和适当的正文来指示需要登录,或者 302 临时重定向到登录页面。

403 I believe is technically correct (and probably most effective if you are implementing a custom API / protocol).

401 is not appropriate as it refers to authorization with a WWW-Authenticate header, which a session cookie is not.

If this is a public facing website where you are trying to deny access based on a session cookie, 200 with an appropriate body to indicate that log in is needed or a 302 temporary redirect to a log in page is often best.

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