zoukankan      html  css  js  c++  java
  • django drf Token验证

    https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication

    1.INSTALLED_APPS

    INSTALLED_APPS = (
        ...
        'rest_framework.authtoken'
    )

    2.REST_FRAMEWORK配置

    REST_FRAMEWORK = {
        # 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
        # 'PAGE_SIZE':2,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
            'rest_framework.authentication.TokenAuthentication'
        )
    }

    3.migrate生成表

    4.创建一个token

    import sys
    import os
    
    pwd = os.path.dirname(os.path.realpath(__file__))
    sys.path.append(pwd)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')
    
    import django
    django.setup()
    
    from rest_framework.authtoken.models import Token
    
    from django.contrib.auth import authenticate
    user = authenticate(username='admin',password='admin')
    token = Token.objects.create(user=user)
    print(token.key)

    5.生成oken

    6.验证token

    可通过request.user查看

    PS:Settings.py中的TokenAuthorazition是全局的,我们也可以取消全局,只在徐娅授权的View中添加authorazition

    1.取消settings中配置

    REST_FRAMEWORK = {
        # 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
        # 'PAGE_SIZE':2,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
            # 'rest_framework.authentication.TokenAuthentication'
        )
    }

    2.在View中配置

    class GoodsList(mixins.ListModelMixin,mixins.CreateModelMixin, viewsets.GenericViewSet):
        class GoodsPagination(PageNumberPagination):
            page_size = 2
            page_size_query_param = 'pageSize'
            page_query_param = 'p'
            max_page_size = 100
    
        queryset = Goods.objects.all()  # 不能切片后再过滤,例如:Goods.objects.all()[:10]
        serializer_class = GoodsSerializer
        pagination_class = GoodsPagination
        authentication_classes = (TokenAuthentication,)
        filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
        search_fields = ('=name',)  # 文档:https://www.django-rest-framework.org/api-guide/filtering/#searchfilter
        ordering_fields = ('name',)
        # filter_fields = ('name',) #逗号必加,缺点无法模糊查询
        filterset_class = GoodsFilter
  • 相关阅读:
    【遇见时光】笔试-偶数最大距离-java
    【遇见时光】笔试-下蹲-java实现
    [遇见时光]内推笔试-藏宝图-java实现
    【遇见时光】内推编程-洗牌
    [遇见时光]剑指offer-顺时针打印矩阵java
    [遇见时光]解决‘javac’不是内部或外部命令的问题
    [遇见时光]Java多线程
    Android Studio 打包错误
    android 开发不常用技巧集
    异步任务结束操作,对于ativity fragment SharedPreferences等涉及到context操作前的判断
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10658468.html
Copyright © 2011-2022 走看看