import jwt # from jwt import exceptions from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions import AuthenticationFailed class JwtAutentication(BaseAuthentication): def authenticate(self, request): token = request.META.get('HTTP_ACCESSTOKEN') SALT = 'django-insecure-zo64fvv02msf-se7!dek5*w$17#3nh6zta#!i=79bt9d#f88@i' try: payload = jwt.decode(token, SALT, True) # except exceptions.ExpiredSignatureError: # raise AuthenticationFailed({'code': 1003, "error": "token已失效"}) except jwt.DecodeError: raise AuthenticationFailed({'code': 1005, 'error': "token认证失败"}) except jwt.InvalidTokenError: raise AuthenticationFailed({'code': 1003, 'error': "非法token"}) return