标签:用户 port 添加 tuple 应用 mic style cep mamicode
authenticate。py文件
import jwt from rest_framework_jwt.authentication import BaseJSONWebTokenAuthentication from rest_framework_jwt.authentication import get_authorization_header from rest_framework.exceptions import AuthenticationFailed from rest_framework_jwt.settings import api_settings #是因为这句话 USER_SETTINGS = getattr(settings, ‘JWT_AUTH‘, None) #api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS) from rest_framework_jwt.authentication import jwt_decode_handler class JSONWebTokenAuthentication(BaseJSONWebTokenAuthentication): def authenticate(self, request): # 采用drf获取token的手段 -HTTP_AUTHORIZATION -Authorization jwt_value = get_authorization_header(request) #自定义从去请求头的某个字段得到认证的token # get_jwt_value--> JSONWebTokenAuthentication(get_jwt_value)--> get_authorization_header --> # from rest_framework.authentication import (BaseAuthentication, get_authorization_header) if not jwt_value: raise AuthenticationFailed(‘Authorization 字段是必须填写的‘) #可以添加反扒措施:原功能是token有前缀 jwt_value_list= jwt_value.split() if len(jwt_value_list) != 2: raise AuthenticationFailed(‘认证失败‘) #默认配置是 ‘JWT_AUTH_HEADER_PREFIX‘: ‘JWT‘, if jwt_value_list[0].lower().decode() != api_settings.JWT_AUTH_HEADER_PREFIX.lower(): raise AuthenticationFailed(‘认证失败‘) jwt_value = jwt_value_list[1] try: payload = jwt_decode_handler(jwt_value) except jwt.ExpiredSignature: raise AuthenticationFailed(‘过期了,认证失败‘) except jwt.InvalidTokenError: raise AuthenticationFailed(‘非法用户,认证失败‘) user = self.authenticate_credentials(payload) return (user, jwt_value) # # class BaseJSONWebTokenAuthentication(BaseAuthentication): # # def authenticate(self, request): # """ # Returns a two-tuple of `User` and token if a valid signature has been # supplied using JWT-based authentication. Otherwise returns `None`. # """ # jwt_value = self.get_jwt_value(request) # if jwt_value is None: # return None # # try: # payload = jwt_decode_handler(jwt_value) # except jwt.ExpiredSignature: # msg = _(‘Signature has expired.‘) # raise exceptions.AuthenticationFailed(msg) # except jwt.DecodeError: # msg = _(‘Error decoding signature.‘) # raise exceptions.AuthenticationFailed(msg) # except jwt.InvalidTokenError: # raise exceptions.AuthenticationFailed() # # user = self.authenticate_credentials(payload) # # return (user, jwt_value)
settings中的配置
#drf_jwt配置 REST_FRAMEWORK = { ‘DEFAULT_AUTHENTICATION_CLASSES‘: [ # ‘rest_framework_jwt.authentication.JSONWebTokenAuthentication‘, #使用默认配置 #使用自定义全局认证 ‘user.authentication.JSONWebTokenAuthentication‘, #user是应用名下的authentication文件 ], } #配置jwt_auth import datetime JWT_AUTH = { #过期时间 ‘JWT_EXPIRATION_DELTA‘: datetime.timedelta(seconds=300), #反扒的请求头 Authorization:gk token ‘JWT_AUTH_HEADER_PREFIX‘ :‘gk‘,
}
局部使用与禁用:任何一个cbv类首行
# 局部禁用 authentication_classes = [] # 局部启用 from user.authentications import JSONWebTokenAuthentication authentication_classes = [JSONWebTokenAuthentication]
内部authentication。py内部
实际认证走的流程
重要的看这里
标签:用户 port 添加 tuple 应用 mic style cep mamicode
原文地址:https://www.cnblogs.com/lakei/p/11215829.html