【问题标题】:Authenticating Access to Django Rest Framework using custom authentication from a Django App使用来自 Django 应用程序的自定义身份验证来验证对 Django Rest Framework 的访问
【发布时间】:2019-10-19 04:16:06
【问题描述】:

我在我的项目的一个(仅)Django 应用程序中实现了自定义身份验证系统 现在我想将我的网站打开到 Api 访问,有没有办法只让 Django 应用程序中的用户访问这个 api。因为我不想重复自己(DRY)所以问是否可以向后工作而不是用非常相似的代码覆盖 Django 休息身份验证

【问题讨论】:

    标签: django django-rest-framework ldap django-authentication django-rest-auth


    【解决方案1】:

    DRF 的SessionAuthentication 包含在默认的 DRF 设置中,对已经熟悉登录您网站的用户完全透明。您可以将其添加到 DRF 的身份验证器列表中:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            # ...
            'rest_framework.authentication.SessionAuthentication',
        )
    }
    

    用户在登录到您的站点时建立的会话现在还针对 DRF 的可浏览 API 和任何 API 调用对他们​​进行身份验证。

    更多信息:https://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication


    如果您问的是如何处理问题API 密钥,DRF 的TokenAuthentication 可以为您解决。您只需向您的网站添加一个视图,允许用户检索他们生成的令牌。

    更多信息:https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 2015-11-12
      • 2019-11-20
      • 2017-01-07
      • 2021-04-04
      • 2019-02-02
      • 2020-03-16
      • 2019-05-09
      • 1970-01-01
      相关资源
      最近更新 更多