我是 Django 新手。我有一个项目,手机可以使用 token 与服务器进行交互。在 settings.py 中我有:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'UPLOADED_FILES_USE_URL': False,
'DEFAULT_PAGINATION_CLASS': None,
'PAGE_SIZE': DEFAULT_PAGE_SIZE, # For views using PageNumberPagination
}
但是当使用 postman 时,我发送带有无效 token 的请求,而不是 401(未经授权),而是返回 403(禁止)。我可以采取什么特殊措施来解决这个问题吗?
tnx
最佳答案
正如documentation所述:
The kind of response that will be used depends on the authentication scheme. Although multiple authentication schemes may be in use, only one scheme may be used to determine the type of response. The first authentication class set on the view is used when determining the type of response.
添加来自 SessionAuthentication 的摘录
Unauthenticated responses that are denied permission will result in an HTTP 403 Forbidden response.
你已经有了答案。
将 TokenAuthentication
移至第一个 DEFAULT_AUTHENTICATION_CLASSES
或记录当前行为。
关于python - djangorest框架授权返回错误的状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48656995/