【问题标题】:custom authentication not working in django-tastypie自定义身份验证在 django-tastypie 中不起作用
【发布时间】:2016-11-30 22:37:25
【问题描述】:

我的问题是,我如何正确编写自己的自定义身份验证??

我试图遵循这个: http://django-tastypie.readthedocs.org/en/latest/authentication.html#implementing-your-own-authentication-authorization

我已经实现了基本方法,

api.py

def prepareResponce(responceData):
    """Prepares a Json responce with status 200"""
    response = JsonResponse(responceData)
    return response  # {"foo": "bar"}

class CustomBasicAuthentication(BasicAuthentication):
    userID = None
    userType = None
    userAccess = None
    userName = None

    def is_authenticated(self, request, **kwargs):
        if 'admin' in request.user.username:
             return prepareResponce({'logged in': 'Admin' })
                  #return True
        return prepareResponce({'not allowed for':userName })


    def get_identifier(self, request):
        return request.user.username


class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        authentication = CustomBasicAuthentication()
        allowed_methods = ['get', 'post']

当我调用提供管理员用户名和密码的 API 时,它总是返回 else 部分。 我哪里做错了?

【问题讨论】:

    标签: python django authentication tastypie


    【解决方案1】:

    你错过了return并且你没有调用父is_authenticated函数:

    def is_authenticated(self, request, **kwargs):
        super(CustomBasicAuthentication, self).is_authenticated(request, **kwargs)
        if 'admin' == request.user.username:
             return prepareResponce({'logged in': 'Admin' })
        return prepareResponce({'not allowed for': self.userName })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2019-01-23
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多