Django REST Framework API Key 项目教程

Django REST Framework API Key 项目教程

djangorestframework-api-key🔐 API key permissions for Django REST Framework项目地址:https://gitcode.com/gh_mirrors/dj/djangorestframework-api-key

1. 项目的目录结构及介绍

djangorestframework-api-key/
├── djangorestframework_api_key/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── checks.py
│   ├── compat.py
│   ├── config.py
│   ├── management/
│   │   └── commands/
│   │       └── generate_api_key.py
│   ├── migrations/
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── permissions.py
│   ├── serializers.py
│   ├── tests/
│   │   ├── __init__.py
│   │   ├── test_admin.py
│   │   ├── test_checks.py
│   │   ├── test_compat.py
│   │   ├── test_config.py
│   │   ├── test_management.py
│   │   ├── test_models.py
│   │   ├── test_permissions.py
│   │   └── test_serializers.py
│   └── views.py
├── docs/
│   ├── conf.py
│   ├── index.rst
│   └── make.bat
├── examples/
│   ├── api_key_example/
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── manage.py
├── LICENSE
├── MANIFEST.in
├── README.md
├── requirements.txt
├── setup.cfg
├── setup.py
└── tox.ini

目录结构介绍

  • djangorestframework_api_key/: 核心代码目录,包含模型、权限、管理界面等。
    • admin.py: 管理界面的配置。
    • models.py: API Key 模型定义。
    • permissions.py: 权限类定义。
    • migrations/: 数据库迁移文件。
    • tests/: 测试代码。
  • docs/: 文档目录,包含 Sphinx 文档配置和源文件。
  • examples/: 示例项目目录,包含一个简单的 Django 项目示例。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • setup.py: 安装脚本。

2. 项目的启动文件介绍

示例项目启动文件

examples/api_key_example/ 目录下:

  • manage.py: Django 项目管理脚本,用于启动开发服务器、运行迁移等。
  • wsgi.py: WSGI 应用程序入口点,用于生产环境部署。

使用方法

  1. 进入示例项目目录:
    cd examples/api_key_example/
    
  2. 启动开发服务器:
    python manage.py runserver
    

3. 项目的配置文件介绍

示例项目配置文件

examples/api_key_example/ 目录下:

  • settings.py: Django 项目配置文件,包含数据库配置、应用注册、中间件等。

关键配置项

  1. 应用注册

    INSTALLED_APPS = [
        ...
        'rest_framework',
        'rest_framework_api_key',
        ...
    ]
    
  2. 权限配置

    REST_FRAMEWORK = {
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework_api_key.permissions.HasAPIKey',
        ]
    }
    
  3. 数据库配置(示例使用 SQLite):

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
    

通过以上配置,可以启用 djangorestframework-api-key 并进行 API Key 认证。

djangorestframework-api-key🔐 API key permissions for Django REST Framework项目地址:https://gitcode.com/gh_mirrors/dj/djangorestframework-api-key

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Django REST framework (DRF) 可以使用 Token 认证、Session 认证和 JSON Web Token (JWT) 认证等方式进行登录验证。 其中,Token 认证和 Session 认证是最基本的认证方式,它们都使用了 Django 自带的用户认证系统,适合于简单的应用场景。Token 认证是通过在请求头中添加 Token 来进行认证,而 Session 认证则是在 Cookie 中保存 Session ID 来进行认证。 JWT 认证则是一种更加灵活和安全的认证方式,它使用了基于 JSON 的 Token,可以完全脱离 Django 自带的用户认证系统,支持跨域访问和分布式系统。JWT 认证需要在 DRF 中添加相应的中间件和配置,同时也需要在前端实现 Token 的生成和保存。 在 DRF 中使用 Token 认证或 Session 认证,只需要在 settings.py 文件中添加相应的认证方式,如: ```python REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ], } ``` 在视图函数中使用 `@authentication_classes` 装饰器来指定认证方式,如: ```python from rest_framework.decorators import authentication_classes from rest_framework.authentication import TokenAuthentication, SessionAuthentication @authentication_classes([TokenAuthentication, SessionAuthentication]) @api_view(['GET']) def my_view(request): # ... ``` JWT 认证需要使用第三方库 `djangorestframework-jwt`,并在 settings.py 文件中添加相应的配置,如: ```python INSTALLED_APPS = [ # ... 'rest_framework', 'rest_framework_jwt', ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', ), } JWT_AUTH = { 'JWT_SECRET_KEY': 'your_secret_key', 'JWT_ALGORITHM': 'HS256', 'JWT_ALLOW_REFRESH': True, 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7), 'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=30), } ``` 在视图函数中使用 `@jwt_authetication_classes` 装饰器来指定 JWT 认证方式,如: ```python from rest_framework_jwt.authentication import JSONWebTokenAuthentication from rest_framework.decorators import jwt_authetication_classes @jwt_authetication_classes([JSONWebTokenAuthentication]) @api_view(['GET']) def my_view(request): # ... ``` 以上是 DRF 中常见的登录验证方式,可以根据具体的应用场景选择合适的认证方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

祖然言Ariana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值