Django REST框架身份验证关键字
我正在尝试将Rest_framework TokenAuthentication关键字从“Token”重命名为“Bearer”,正如文档中所建议的那样,我对TokenAuthentication类进行了如下子类划分: 在模块中:user/authentication.pyDjango REST框架身份验证关键字,django,authentication,django-rest-framework,token,bearer-token,Django,Authentication,Django Rest Framework,Token,Bearer Token,我正在尝试将Rest_framework TokenAuthentication关键字从“Token”重命名为“Bearer”,正如文档中所建议的那样,我对TokenAuthentication类进行了如下子类划分: 在模块中:user/authentication.py from rest_framework import authentication class TokenAuthentication(authentication.TokenAuthentication): """
from rest_framework import authentication
class TokenAuthentication(authentication.TokenAuthentication):
"""
Simple token based authentication.
Clients should authenticate by passing the token key in the "Authorization"
HTTP header, prepended with the string "Token ". For example:
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
"""
keyword = 'Bearer'
模块内app/settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'user.authentication.TokenAuthentication',
),
}
当我使用'Authorization:Bearer…token…'而不是'Authorization:token…token…'时,它仍然向我发送401未经授权的邮件
我做错了什么?你错过了最后一步。导入
TokenAuthentication
类,该类在视图中包含新关键字,而不是默认的TokenAuthentication
类 从rest\u框架导入身份验证
类TokenAuthentication(authentication.TokenAuthentication):
你也可以发布你的视图代码吗?谢谢你的建议,我还在每个ViewOdd的authentication_类中使用了TokenAuthentication。这就是我为整个项目更改TokenAuthentication
所需的全部操作。我不需要在视图中添加任何内容。这里有相同的问题吗?你找到解决办法了吗?
authentication.TokenAuthentication.keyword = 'Bearer'