django rest_framework 前端网页实现Token认证

rest_framework提供了几种认证方式:Session、Token等。Session是最简单的,几乎不用写任何代码就可以是实现,Token方式其实也不复杂,网上的教程一大把,但是最后都是用Postman这类工具来实现API调用的,通过这类工具来增加HTTP头信息以回传Token。那么真正的前端网页应该怎么办呢?网上基本上就是基于Aixos来实现的,但是我就不想用Vue,纯Javascript是不能改写HTTP头的。怎么办?

首先,看一下TokenAuthentication的源码:

复制代码
auth = request.META.get('HTTP_AUTHORIZATION', b'')

它是从HTTP头中读取Authorization,其中是的内容Token(固定字) Token(值)。

那既然JavaScript不能改写头,但是能用cookie啊,所以我就通过自定义TokenAuthentication类,读取Cookie来实现。

  1. 用户登录之后先创建Token,并返回给前端。

  2. 前端把Token保存到Cookie。

复制代码
$.ajax({
    url: "login",
    type:'POST',
    data: {'username':username,'password':password},
    dataType: "json",
    success: function (data) {   //请求成功后执行的操作
        if (data.status == "SUCCESS") {
            //localStorage.setItem('token',data.token);
            $.cookie('token', data.token);
            window.location.href = '/';
        }
        else{
            alert('Invalid username or password.');
        }
    }
});

这样每次发起HTTP请求时都会把Token带上

  1. 在logout的时候把Token删除。防止失效Token仍然可以使用。
复制代码
def logout_view(request):
    request.user.auth_token.delete()
    logout(request)

    return redirect('login')
  1. 写一个自定义的TokenAuthentication类:
复制代码
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.authtoken.models import Token

class CustomTokenAuthentication(BaseAuthentication):
    keyword = 'token'

    def authenticate(self, request):
        cookie_token = request.COOKIES.get(self.keyword)

        if cookie_token is None:
            raise AuthenticationFailed('No Token Found in Cookies!')

        try:
            user_token = Token.objects.get(key=cookie_token)
            if user_token is None:
                raise AuthenticationFailed('No Token Found for Current User!')

            return (user_token.user, user_token)
        except Token.DoesNotExist:
            raise AuthenticationFailed('Token in cookie is invalidate!')

当Token未提供或者无效时,直接抛出AuthenticationFailed异常

  1. 讲自定义的类放入项目的settings,否则不生效:
复制代码
REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': (
       'bid_request_system_app.commons.authentications.CustomTokenAuthentication',
       'rest_framework.authentication.SessionAuthentication',
   ),
}
  1. 在相对应的view方法前面加上相应的注解:
复制代码
@api_view(['GET', 'POST'])
@csrf_exempt
@login_required()
@authentication_classes([CustomTokenAuthentication])
@permission_classes([IsAuthenticated, IsAdminUser])
def department_management_view(request):

这样的话就OK了。

相关推荐
知秋丶6 分钟前
大模型应用发展与Agent前沿技术趋势(下)
人工智能·python·ai agent
2401_8370885032 分钟前
ref 简单讲解
前端·javascript·vue.js
折果1 小时前
如何在vue项目中封装自己的全局message组件?一步教会你!
前端·面试
HenryLin1 小时前
美股量化分析系统 - 模块调用流程文档
python
不死鸟.亚历山大.狼崽子1 小时前
Syntax Error: Error: PostCSS received undefined instead of CSS string
前端·css·postcss
汪子熙1 小时前
Vite 极速时代的构建范式
前端·javascript
跟橙姐学代码1 小时前
一文读懂 Python 的 JSON 模块:从零到高手的进阶之路
前端·python
躺不平的小刘2 小时前
从YOLOv5到RKNN:零冲突转换YOLOv5模型至RK3588 NPU全指南
linux·python·嵌入式硬件·yolo·conda·pyqt·pip
前端小巷子2 小时前
Vue3的渲染秘密:从同步批处理到异步微任务
前端·vue.js·面试
文火冰糖的硅基工坊2 小时前
[激光原理与应用-317]:光学设计 - Solidworks - 草图
开发语言·python·信息可视化·系统架构