Python Django REST Framework';令牌身份验证
在使用身份验证构建的API中,可以使用TokenAuthentication方法完成身份验证。Its表示身份验证令牌应该通过Python Django REST Framework';令牌身份验证,python,django,authentication,django-rest-framework,Python,Django,Authentication,Django Rest Framework,在使用身份验证构建的API中,可以使用TokenAuthentication方法完成身份验证。Its表示身份验证令牌应该通过授权头发送 通常可以通过查询字符串发送API密钥或令牌以进行身份验证,如https://domain.com/v1/resource?api-键=拉拉 Django REST Framework的TokenAuthentication也有同样的方法吗?由Deafolt开发的DRF不支持查询字符串进行身份验证,但您可以轻松地重写TokenAuthentication类中的身份
授权
头发送
通常可以通过查询字符串发送API密钥或令牌以进行身份验证,如https://domain.com/v1/resource?api-键=拉拉
Django REST Framework的TokenAuthentication也有同样的方法吗?由Deafolt开发的DRF不支持查询字符串进行身份验证,但您可以轻松地重写
TokenAuthentication
类中的身份验证
方法来支持它
例如:
class TokenAuthSupportQueryString(TokenAuthentication):
"""
Extend the TokenAuthentication class to support querystring authentication
in the form of "http://www.example.com/?auth_token=<token_key>"
"""
def authenticate(self, request):
# Check if 'token_auth' is in the request query params.
# Give precedence to 'Authorization' header.
if 'auth_token' in request.QUERY_PARAMS and \
'HTTP_AUTHORIZATION' not in request.META:
return self.authenticate_credentials(request.QUERY_PARAMS.get('auth_token'))
else:
return super(TokenAuthSupportQueryString, self).authenticate(request)
class TokenAuthSupportQueryString(TokenAuthentication):
"""
扩展TokenAuthentication类以支持querystring身份验证
以“的形式”http://www.example.com/?auth_token="
"""
def身份验证(自我、请求):
#检查“token_auth”是否在请求查询参数中。
#优先考虑“授权”标题。
如果request.QUERY参数中的“auth_token”和\
“HTTP_授权”不在request.META中:
返回self.authenticate_凭据(request.QUERY_PARAMS.get('auth_token'))
其他:
返回超级(TokenAuthSupportQueryString,self)。身份验证(请求)
DRF
具有TokenAuthentication
功能,可在标题中查找token
。方法authenticate\u credentials
负责验证令牌。从2018年起,使用Django Rest框架,您可以创建自己的身份验证类,请参阅
然后在APIView类中添加
authentication\u classes=(JSONWebTokenAuthenticationQS,)
或
@authentication\u类((JSONWebTokenAuthenticationQS,)
关于视图函数 请参考OmriToptix
和Scott Warren
以及其他网站和
对于现在大多数情况,请使用JSONWebTokenAuthentication
,因此现在应覆盖其get\u jwt\u值,完整代码为:
# from rest_framework_jwt.authentication import BaseJSONWebTokenAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import get_authorization_header
class JWTAuthByQueryStringOrHeader(JSONWebTokenAuthentication):
# class JWTAuthByQueryStringOrHeader(BaseJSONWebTokenAuthentication):
"""
Extend the TokenAuthentication class to support querystring authentication
in the form of "http://www.example.com/?jwt_token=<token_key>"
"""
def get_jwt_value(self, request):
# Check if 'jwt_token' is in the request query params.
# Give precedence to 'Authorization' header.
queryParams = request.query_params
reqMeta = request.META
if ('jwt_token' in queryParams) and ('HTTP_AUTHORIZATION' not in reqMeta):
jwt_token = queryParams.get('jwt_token')
# got jwt token from query parameter
return jwt_token
else:
# call JSONWebTokenAuthentication's get_jwt_value
# to get jwt token from header of 'Authorization'
return super(JWTAuthByQueryStringOrHeader, self).get_jwt_value(request)
现在前端/web端可以这样调用api:
http://localhost:65000/api/v1/scripts/3d9e77b0-e538-49b8-8790-60301ca79e1d/script_word_export/?jwt_token=EYJ0Exhioijkv1qilcjHbgChiuzi1Nij9.EYJ1C2Vyx2LkijoimWKmgeZdGTMMFiyi00mdfkltK5NtyTq5MZcXndiWuziiW5HBwuxHSC2W5JZw50iIwizwijjjJjHbJnTmxOtayotu0LCjLbFfBCI6inzPBMNNqHy2BKbKmKmKmKmKmKmKmKmKmKmKmK9KmKmKfY9Fy8Fy8FfY8FfY8FY8FY-IQ5a8n_8OplbYkj7s
要将jwt_令牌传递到服务器端,请获得下载/导出文件的授权
虽然仍然支持原始方法在头“授权”中传递jwt令牌
:
POST http://localhost:65000/api/v1/scripts/3d9e77b0-e538-49b8-8790-60301ca79e1d/script_word_export/
Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMWVkMGEwZDgtMmFiYi00MDFkLTk5NTYtMTQ5MzcxNDIwMGUzIiwidXNlcm5hbWUiOiJsc2R2aW5jZW50IiwiZXhwIjoxNTMxOTAyOTU0LCJlbWFpbCI6InZpbmNlbnQuY2hlbkBuYXR1cmxpbmcuY29tIn0.wheM7Fmv8y8ysz0pp-yUHFqfk-IQ5a8n_8OplbYkj7s
跟进斯科特·沃伦的回答。这是朝着正确方向迈出的一步,因为DRFJWT文档不包括重要的身份验证类行。但是,正如中所指出的,有一个大问题,即不能混合使用JWT身份验证方法。我的结局是:
class JSONWebTokenAuthenticationQS(JSONWebTokenAuthentication):
def get_jwt_value(self, request):
return request.GET.get('jwt') or JSONWebTokenAuthentication.get_jwt_value(self, request)
到目前为止,这似乎效果不错。它使用JSONWebTokenAuthentication而不是基类,因为它必须使用原始的get\u jwt\u value方法。以防万一,TokenAuthentication
类从rest\u框架导入。authentication导入TokenAuthentication
。另外,不要忘记在设置中将其添加为DEFAULT\u AUTHENTICATION\u CLASSES
,或者使用AUTHENTICATION\u CLASSES=(TokenAuthSupportQueryString,)
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'apps.util.jwt_token.JWTAuthByQueryStringOrHeader',
...
),
...
}
POST http://localhost:65000/api/v1/scripts/3d9e77b0-e538-49b8-8790-60301ca79e1d/script_word_export/
Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMWVkMGEwZDgtMmFiYi00MDFkLTk5NTYtMTQ5MzcxNDIwMGUzIiwidXNlcm5hbWUiOiJsc2R2aW5jZW50IiwiZXhwIjoxNTMxOTAyOTU0LCJlbWFpbCI6InZpbmNlbnQuY2hlbkBuYXR1cmxpbmcuY29tIn0.wheM7Fmv8y8ysz0pp-yUHFqfk-IQ5a8n_8OplbYkj7s
class JSONWebTokenAuthenticationQS(JSONWebTokenAuthentication):
def get_jwt_value(self, request):
return request.GET.get('jwt') or JSONWebTokenAuthentication.get_jwt_value(self, request)