最近用django 来实现JWT验证的时候发现, 使用django框架提取头部key的时候框架会自己加一些神奇的地方:
如果用 self.request.META:
自己定义的变量会变成
user -> HTTP_USER
user-name -> HTTP_USER_NAME
如果使用self.request.headers
定义的变量会变成
user -> User
user-name ->UserName 双峰命名
因此我关于认证头部的中间件代码会变为:
class JWTQueryParamsAuthentication(BaseAuthentication):
def authenticate(self, request):
info = request.META.get('HTTP_AUTHORIZATION','')
# info = request.headers
# print(info)
#
#
# print(info)
auth = info.split()
if not auth:
raise AuthenticationFailed({'msg': '未获取到请求头', 'code': 403})
if auth[0].lower() != 'jwt':
raise AuthenticationFailed({'msg': '请求头认证方式错误', 'code': 403})
if len(auth) ==