我的代码有两个mixin,BasicAuthMixin和JWTAuthMixin如下所述。姑且认为self.authenticate方法返回真并不会引发任何异常:Django调用基于Mixin的另一个类基于Mixin
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
class BasicAuthMixin(View):
"""
Add this mixin to the views where Basic Auth is required.
"""
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
try:
self.authenticate(request)
except:
return JsonResponse({'status': 403, 'message': 'Forbidden'}, status=403, content_type='application/json')
return super(BasicAuthMixin, self).dispatch(request, *args, **kwargs)
class JWTAuthMixin(View):
"""
Add this mixin to the views where JWT based authentication is required.
"""
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
try:
self.authenticate(request)
except:
return JsonResponse({'status': 403, 'message': 'Forbidden'}, status=403, content_type='application/json')
return super(JWTAuthMixin, self).dispatch(request, *args, **kwargs)
这些混入在根据需要的认证的意见被使用。
的实际问题,从这里开始:我试图创建另一个混入AllAuthMixin,其中包括在任何视图中会自动决定哪些混入需要被称为基于认证报头时提供:
class AllAuthMixin(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
auth = request.META.get('HTTP_AUTHORIZATION') or ''
if auth.startswith('Bearer'):
return JWTAuthMixin.as_view()(request, *args, **kwargs)
elif auth.startswith('Basic'):
return BasicAuthMixin.as_view()(request, *args, **kwargs)
raise Exception('Unauthorized Access to Saurav APIs', 403)
一旦我有AllAuthMixin在任何观点说/测试它实际上调用适当的混入,但返回不允许的方法(GET):/测试
我调试,发现不允许的方法错误消息从以下行来,如果我使用基本身份验证:
return super(BasicAuthMixin, self).dispatch(request, *args, **kwargs)
以下说明了一个非常简单的例子来叫我的看法与基本身份验证:
>>> import requests
>>> requests.get('http://127.0.0.1:8000/test', auth=('UserName', 'Password'))
<Response [405]>
我不知道我在做什么错在这里。任何人都可以请帮我找出问题或任何其他方式来实现这一点。我想要的是重用已经声明的mixin:BasicAuthMixn和JWTAuthMixin。
让我们[继续聊天讨论](http://chat.stackoverflow.com/rooms/156110/discussion-between-saurav-kumar-and-yeray-diaz-diaz)。 –
**调度**每个mixin的方法正在做很多我在这里没有提到的事情;如验证,引发异常,以适当的响应代码返回适当的json响应。不调用调度方法,导致我在** AllAuthMixin **中写入重复代码。但那很好! **你最近的建议起作用**,至少我不打扰现有的代码。非常感谢您宝贵的时间。非常感谢 :) –