DRF修改权限、用户认证方式

简介: 修改权限、用户认证方式在settings中全局配置REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.

修改权限、用户认证方式

  • 在settings中全局配置

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
        ]
    }
  • 基于每个视图集进行配置

    from rest_framework.authentication import SessionAuthentication, BasicAuthentication
    from rest_framework.permissions import IsAuthenticated
    from rest_framework.response import Response
    from rest_framework.views import APIView
    
    class ExampleView(APIView):
        authentication_classes = [SessionAuthentication, BasicAuthentication]
        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)
  • 配合@api_view装饰器基于函数一起使用

    from rest_framework.decorators import permission_classes, authentication_classes
    class ExampleView(APIView):
    
        @api_view(['GET'])
        @authentication_classes([SessionAuthentication, BasicAuthentication])
        @permission_classes([IsAuthenticated])
        def example_view(request, format=None):
            content = {
                'user': unicode(request.user),  # `django.contrib.auth.User` instance.
                'auth': unicode(request.auth),  # None
            }
            return Response(content)
  • 配合@action装饰器基于函数一起使用

    
    class ExampleView(APIView):
    
        @action(methods=['get'], detail=False, url_path='url', url_name='url', permission_classes=(IsAuthenticated,), authentication_classes=(SessionAuthentication, BasicAuthentication))
        def example_view(request, format=None):
            content = {
                'user': unicode(request.user),  # `django.contrib.auth.User` instance.
                'auth': unicode(request.auth),  # None
            }
            return Response(content)

参考链接:


相关文章
|
9月前
|
数据安全/隐私保护
fastadmin是如何设置没有权限的用户不能访问某些页面的?
fastadmin是如何设置没有权限的用户不能访问某些页面的?
237 0
|
4月前
|
前端开发 Java 数据库
基于RBAC的权限模型+shiro+springboot实现的系统登陆权限认证模块
基于RBAC的权限模型+shiro+springboot实现的系统登陆权限认证模块
44 0
|
8月前
|
前端开发 数据库 数据安全/隐私保护
DRF--认证和权限
DRF--认证和权限
|
11月前
|
数据库 Python
django drf 实现只有超级用户才能注册账号(涉及自定义权限permissions,获取token信息解析token信息)
django drf 实现只有超级用户才能注册账号(涉及自定义权限permissions,获取token信息解析token信息)
|
存储 缓存 数据安全/隐私保护
Jasny SSO是如何处理用户会话的?底层原理是什么?
Jasny SSO是如何处理用户会话的?底层原理是什么?
|
API 数据安全/隐私保护
Yii2.0框架中如何进行身份验证和授权操作?支持哪些认证方式和授权方式?
Yii2.0框架中如何进行身份验证和授权操作?支持哪些认证方式和授权方式?
132 0
|
Linux 数据安全/隐私保护
设计并实现请求路径权限认证
设计并实现请求路径权限认证
设计并实现请求路径权限认证
|
数据库 数据安全/隐私保护 Python
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
226 0
【Django学习笔记 - 19】:认证、自定义认证、权限、限流
|
存储 JSON 中间件
8. 为Lamb编写用户登录接口(带token)
上篇我们给Lamb引入了flask_sqlalchemy,接着我们就编写我们第一个接口---登录
121 0
8. 为Lamb编写用户登录接口(带token)
|
NoSQL 前端开发 Redis
简单的token用户登录实现,接口权限校验
简单的token用户登录实现,接口权限校验