jwt认证生成后的token后端解析
一.首先前端发送token
token所在的位置headers
{'authorization':token的值',Content-Type':application/json}
在ajax写//只展示headers部分代码headers:{"authorization":this.$cookies.get("token")}//token值一般是放在cookies里面//ajax提交默认就是json格式所有不需要声明js格式
二.后端接受并解析token
1.首先先定义个认证类
fromrest_framework.exceptionsimportAuthenticationFailedimportjwtfromrest_framework_jwt.authenticationimportBaseJSONWebTokenAuthenticationfromrest_framework_jwt.authenticationimportjwt_decode_handlerfromrest_framework_jwt.authenticationimportget_authorization_headerclass JWTAuthentication(BaseJSONWebTokenAuthentication):# 自定义认证类,重写authenticate方法def authenticate(self, request):# 认证通过,返回user,auth# 认证失败,返回None# auth = request.META.get('HTTP_AUTHORIZATION') # 前台用auth携带token# 通过前台传过来的请求头中获取authauth = get_authorization_header(request)ifnotauth:raiseAuthenticationF