我在我的应用程序中使用Django REST框架。我需要身份验证,但不是默认身份验证。我有一个模型:
class Worker(models.Model):
token = models.CharField(...)
ip = models.GenericIPAddressField(...)
created_date = models.DateTimeField(...)
last_update = models.DateTimeField(...)
Worker通过我的API发送消息来查看WorkerView,它继承了Django REST框架的apieview。令牌在请求的头中发送:
^{pr2}$我有一个认证方法:
def authenticate(request):
try:
ip = request.META.get("REMOTE_ADDR", None)
token = request.META.get("HTTP_AUTHORIZATION", None)
...
我想到了两个解决方案:
创建一个mixin类并在我的WorkerView中继承它:
class WorkerView(AuthenticationMixin, APIView)
...
从我的authenticate方法生成一个类装饰器,并按如下方式使用:
@authenticate
class WorkerView(APIView)
但在这两种情况下,我都需要将请求参数传递给authenticate方法。 怎么做?或者我的问题有更好的解决方案?在
为什么不按照这里的定义创建一个自定义身份验证类? http://www.django-rest-framework.org/api-guide/authentication/#custom-authentication
相关问题 更多 >
编程相关推荐