1.方式一:使用djangorestframework-jwt
1.1 安装
pip install djangorestframework-jwt
1.2 在settings.py中增加如下配置:
![c4a6c040b37ddc14ca5dc688dfb8b312.png](https://img-blog.csdnimg.cn/img_convert/c4a6c040b37ddc14ca5dc688dfb8b312.png)
1.3 在项目的urls.py中增加如下配置:
![588767865ecc7fb987fdd24a2fc85ca1.png](https://img-blog.csdnimg.cn/img_convert/588767865ecc7fb987fdd24a2fc85ca1.png)
1.4 测试
请求头中不加token时,request user和request.auth的值分别是 AnonymousUser和None。
使用post请求,application/json数据格式的方式请求http://127.0.0.1:8000/token/,加入请求参数:
![4f76c0876637346e6b31e9e43c871b55.png](https://img-blog.csdnimg.cn/img_convert/4f76c0876637346e6b31e9e43c871b55.png)
可以获取token字符串:
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI