gpt4 book ai didi

django - 为什么 Django REST Framework 中的 SessionAuthentication 从不返回 HTTP401

转载 作者:行者123 更新时间:2023-12-04 23:21:09 26 4
gpt4 key购买 nike

看着 the docsthe source在 Django REST 框架中,我看到 SessionAuthentication只返回 HTTP 403 代码,而其他 Authentication类将返回 401。这是什么原因?

肯定有很多情况401 makes sense .

该问题尤其成问题,因为“在确定响应类型时使用 View 上设置的第一个身份验证类”。和 SessionAuthentication默认情况下是第一个 Authentication类(class)。

最佳答案

Django REST 框架遵循 HTTP specification , 并且在 Authentication 时不返回 401 响应类不返回 WWW-Authenticate可以使用的 header 。

HTTP 401 responses must always include a WWW-Authenticate header, that instructs the client how to authenticate. HTTP 403 responses do not include the WWW-Authenticate header.

-- Django REST Framework documentation


因为 SessionAuthentication类没有定义 WWW-Authenticate可以使用的 header ,Django REST Framework 不能返回 401 响应并且仍然遵循规范。你可以通过设置另一个 Authentication 来解决这个问题。支持列表顶部标题的类,例如 BasicAuthentication .

关于django - 为什么 Django REST Framework 中的 SessionAuthentication 从不返回 HTTP401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com