python - JWT, how does django customize permission about users?
高洛峰
高洛峰 2017-05-15 17:14:23
0
2
865

When using validation based on cookies, write such a 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

When I use jwt to verify, the login returns a token without running login(request, user), that is, request.user is AnonymousUser.

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

So how should I modify this Permission? Please help.

高洛峰
高洛峰

拥有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