Python Django Rest框架身份验证
我正在尝试验证通过web API接口在url中传递的GUID。但是,我无法将GUID传递给我的身份验证类 注意:通过身份验证,我的意思是确保它是一个有效的GUID 我的URL.py:Python Django Rest框架身份验证,python,django,authentication,django-rest-framework,Python,Django,Authentication,Django Rest Framework,我正在尝试验证通过web API接口在url中传递的GUID。但是,我无法将GUID传递给我的身份验证类 注意:通过身份验证,我的意思是确保它是一个有效的GUID 我的URL.py: url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$', views.CustomerAddressView.as_view()), class CustomerAddressView(
url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$',
views.CustomerAddressView.as_view()),
class CustomerAddressView(generics.RetrieveAPIView):
lookup_field = "address_id"
queryset = CustomerAddress.objects.all()
serializer_class = CustomerAddressSerializer
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'customer.authenticate.Authenticate',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
我的设置.py:
url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$',
views.CustomerAddressView.as_view()),
class CustomerAddressView(generics.RetrieveAPIView):
lookup_field = "address_id"
queryset = CustomerAddress.objects.all()
serializer_class = CustomerAddressSerializer
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'customer.authenticate.Authenticate',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
我的客户应用程序中的身份验证类如下所示:
class Authenticate(authentication.BaseAuthentication) :
def authenticate(self, request):
request = request._request
guid = getattr(request, 'guid', None)
my_logger.debug(guid)
if not guid:
my_logger.debug('There is no guid!')
return None
try:
user = Customer.objects.get(guid=guid,brand=1)
except Customer.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user')
return None
请求如下所示:
class Authenticate(authentication.BaseAuthentication) :
def authenticate(self, request):
request = request._request
guid = getattr(request, 'guid', None)
my_logger.debug(guid)
if not guid:
my_logger.debug('There is no guid!')
return None
try:
user = Customer.objects.get(guid=guid,brand=1)
except Customer.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user')
return None
问题:
我喜欢在类身份验证中检索GUID,并确保其有效。目前,我不断收到您在屏幕截图中看到的错误,我的日志显示:“没有guid!”
如何将guid从请求传递给Authenticate类
谢谢您可以这样做:
class Authenticate(authentication.BaseAuthentication) :
def authenticate(self, request):
request = request._request
# This is a bit hacky way to get value of guid
guid = request.path.split('/')[-3]
my_logger.debug(guid)
if not guid:
my_logger.debug('There is no guid!')
return None
try:
user = Customer.objects.get(guid=guid,brand=1)
except Customer.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user')
return None
这有点不正常,因为我试图通过拆分request.path
about'/'
来访问guid
,并访问拆分后获得的列表的最后三个索引
我已经检查过,self
无法访问我们通常在DRF视图中获得的kwargs
,因此我们无法访问这里的kwargs
另一种解决方案是,当通过重写DRF的身份验证过程调用
authenticate()
时,在DRF视图中显式传递kwargs
。检查您是否有权在self中访问kwargs
。Doguid=self.kwargs.get('guid',None)
我已经检查过了,您在authenticate()
中没有DRF视图的kwargs
。我已经更新了我的ans。另一个解决方案是在调用时将视图的kwargs
传递到authenticate()
。