关于django-rest-framwork中集成json web token机制

本文不讲解具体原理,只编写操作步骤
**

1.安装json web token

**
通过pip 安装
pip install djangorestframework-jwt -i https://mirrors.aliyun.com/pypi/simple/ ( 此处用的是阿里的pip源 -下载速度较快)

2.在已经创建好的Django项目中的setting文件中进行配置

具体如图

在这里插入图片描述

REST_FRAMEWORK = {
    #  设置所有接口都需要被验证
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

3.设置views视图层

from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
 
class ExampleView(APIView):
    authentication_classes = [SessionAuthentication, JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]
 
    def get(self, request, format=None):
        content = {
            'user': unicode(request.user),  # `django.contrib.auth.User` instance.
            'auth': unicode(request.auth),  # None
        }
        return Response(content)

4.设置url路由 设置在项目的主url中

from rest_framework_jwt.views import obtain_jwt_token
		    url(r'^api-token-auth/', obtain_jwt_token),

在这里插入图片描述

5.启动项目

python manage.py runserver 

6.通过jmeter 调用 这个接口

1.打开jemter 填入url 端口号
2.在参数中添加 username   和password   并输入对应的 用户名(此处的用户名填写能够进入 django admin后台的那个账号)

在这里插入图片描述

3.点击启动,查看结果数,此时token已经给返回
在这里插入图片描述

7.携带后端返回的token进行登录

未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值