【发布时间】:2015-06-15 09:22:09
【问题描述】:
我正在尝试对通过 Web API 接口在 url 中传递的 GUID 进行身份验证。但是,我无法将 GUID 传递给我的 Authenticate 类。
注意:通过身份验证是指确保它是有效的 GUID
我的urls.py:
url(r'^customer_address/(?P<guid>[a-z0-9-]+)/(?P<address_id>[a-z0-9-]+)/$',
views.CustomerAddressView.as_view()),
我的views.py:
class CustomerAddressView(generics.RetrieveAPIView):
lookup_field = "address_id"
queryset = CustomerAddress.objects.all()
serializer_class = CustomerAddressSerializer
我的settings.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'customer.authenticate.Authenticate',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
我的客户应用中的 Authenticate 类如下所示:
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
请求看起来像这样:
问题: 我喜欢在 Authenticate 类中检索 GUID 并确保它有效。目前,我不断收到您在屏幕截图中看到的错误,并且我的日志显示:“没有 guid!”
如何将 guid 从请求传递到 Authenticate 类?
谢谢
【问题讨论】:
-
检查您是否有权访问自己的
kwargs。做guid = self.kwargs.get('guid', None) -
我查过,
authenticate()中没有 DRF 视图的kwargs。我已经更新了我的ans。另一种解决方案是在调用时将视图的kwargs传递给authenticate()。
标签: python django authentication django-rest-framework