Django REST Framework TokenAuthentication 通常在 Django 中进行身份验证
Django REST Framework TokenAuthentication to authenticate in Django generally
我们正在 rest_framework.authentication.TokenAuthentication
使用访问令牌对 Django REST Framework 中的 API 用户进行身份验证。
有没有办法使用同样的 class 来对 Django 的用户进行一般身份验证?
我试过直接将它添加到 AUTHENTICATION_BACKENDS
但它不起作用:
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
'rest_framework.authentication.TokenAuthentication',
)
是否有快速的方法来执行此操作,或者我是否需要编写自定义身份验证后端?
您可以使用 SessionAuthentication。
AUTHENTICATION_BACKENDS = (
# Use Django's session framework for authentication.
'rest_framework.authentication.SessionAuthentication',
....
'rest_framework.authentication.TokenAuthentication',
)
Django REST framework 身份验证和权限 类 需要使用 Django REST framework 视图,因为身份验证是在视图级别完成的 [1]。这与 Django 身份验证后端不同,后者的身份验证是通过中间件完成的。
Is there a way to use this same class to authenticate users for Django generally?
不,Django REST 框架身份验证后端与 Django 身份验证后端截然不同,相反 技术上 正确 [2]。
[1]: 已经讨论过将其移动到中间件级别,但目前没有计划。
[2]:可以通过 SessionAuthentication
和其他类似的 DRF 后端使用 Django 身份验证后端。
我们正在 rest_framework.authentication.TokenAuthentication
使用访问令牌对 Django REST Framework 中的 API 用户进行身份验证。
有没有办法使用同样的 class 来对 Django 的用户进行一般身份验证?
我试过直接将它添加到 AUTHENTICATION_BACKENDS
但它不起作用:
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
'rest_framework.authentication.TokenAuthentication',
)
是否有快速的方法来执行此操作,或者我是否需要编写自定义身份验证后端?
您可以使用 SessionAuthentication。
AUTHENTICATION_BACKENDS = (
# Use Django's session framework for authentication.
'rest_framework.authentication.SessionAuthentication',
....
'rest_framework.authentication.TokenAuthentication',
)
Django REST framework 身份验证和权限 类 需要使用 Django REST framework 视图,因为身份验证是在视图级别完成的 [1]。这与 Django 身份验证后端不同,后者的身份验证是通过中间件完成的。
Is there a way to use this same class to authenticate users for Django generally?
不,Django REST 框架身份验证后端与 Django 身份验证后端截然不同,相反 技术上 正确 [2]。
[1]: 已经讨论过将其移动到中间件级别,但目前没有计划。
[2]:可以通过 SessionAuthentication
和其他类似的 DRF 后端使用 Django 身份验证后端。