python - JWT, django如何定制关于用户的Permission?
高洛峰
高洛峰 2017-05-15 17:14:23
0
2
851

使用基于cookies的验证时,写了这样一个Permission

class IsAuthenticatedAndStudentOwner(BasePermission):
    message = 'You must be a student.'
    def has_permission(self, request, view):

        return request.user.is_authenticated() and smart_str(request.user.identity) == '学生'

    def has_object_permission(self, request, view, obj):

        return obj.student.user == request.user

当我使用jwt验证后,登录返回一个token,不运行login(request, user),也就是request.userAnonymousUser.

# login(request, user_obj)
payload = jwt_payload_handler(user_obj)
token = jwt_encode_handler(payload)
data['token'] = token
return data

那么我该怎么修改这个Permission呢,求解。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
阿神

jwt的验证,你是通过headerORcookie的方式传递的?
define another method in class IsAuthenticatedAndStudentOwner

def is_authenticated(self, request, view):
    if using cookie:
        return request.user.is_authenticated()
    elseif jwt:
        ...

def has_permission(self, request, view):

    return self.is_authenticated(request, view) and smart_str(request.user.identity) == '学生'

    
習慣沉默

You can also open it using jwtlogin(request, user_obj) ah

The back-end user still exists in the request, but when using jwt, the django template is no longer used, and the user cannot be used freely in the page

I will write this soon, please continue to pay attention to the exchange

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template