简体   繁体   English

Django REST Framework TokenAuthentication 一般在 Django 中进行身份验证

[英]Django REST Framework TokenAuthentication to authenticate in Django generally

We're using rest_framework.authentication.TokenAuthentication to authenticate API users in Django REST Framework using an access token.我们正在使用rest_framework.authentication.TokenAuthentication使用访问令牌对 Django REST 框架中的 API 用户进行身份验证。

Is there a way to use this same class to authenticate users for Django generally?有没有办法使用同一个类来验证 Django 的用户?

I've tried adding it straight in to AUTHENTICATION_BACKENDS but it doesn't work:我已经尝试将它直接添加到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',
)

Is there a quick way to do this or do I need to write a custom authentication backend?有没有一种快速的方法可以做到这一点,或者我是否需要编写自定义身份验证后端?

Django REST framework authentication and permission classes require the use of Django REST framework views, as the authentication is done on the view level [1] . Django REST framework 身份验证和权限类需要使用 Django REST framework 视图,因为身份验证是在视图级别完成的[1] This is different from Django authentication backends, where the authentication is done through the middleware.这与 Django 身份验证后端不同,后者通过中间件完成身份验证。

Is there a way to use this same class to authenticate users for Django generally?有没有办法使用同一个类来验证 Django 的用户?

No, Django REST framework authentication backends are distinctly separate from Django authentication backends, and the reverse is technically true [2] .不,Django REST framework 身份验证后端与 Django 身份验证后端明显分开,技术上正好相反[2]

[1]: There has been discussion of moving this to the middleware level, but this is not currently planned. [1]:已经讨论过将其移至中间件级别,但目前还没有计划。
[2]: Django authentication backends can be used through SessionAuthentication and other comparable DRF backends. [2]:Django 身份验证后端可以通过SessionAuthentication和其他类似的 DRF 后端使用。

You can use SessionAuthentication .您可以使用SessionAuthentication

AUTHENTICATION_BACKENDS = (
    # Use Django's session framework for authentication.
    'rest_framework.authentication.SessionAuthentication',
    ....
    'rest_framework.authentication.TokenAuthentication',
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Rest框架TokenAuthentication不起作用 - django rest framework TokenAuthentication not working Django REST框架TokenAuthentication返回匿名用户 - Django REST framework TokenAuthentication returns anonymous user "detail": "方法 \\"GET\\" 不允许。" 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework 使用 Django REST Framework 的 TokenAuthentication 查询字符串中的令牌 - Token in query string with Django REST Framework's TokenAuthentication 如何在django-rest-framework中使用TokenAuthentication for API - How to use TokenAuthentication for API in django-rest-framework 根据请求的类型,在API视图的一部分上应用tokenauthentication Django rest框架 - Apply tokenauthentication Django rest framework on part of the API view based on the type of the request 在 django rest 框架(使用 TokenAuthentication)公共 APIView 上获取 CSRF 令牌丢失错误 - Getting CSRF token missing error on a django rest framework (with TokenAuthentication) public APIView 在 Django Rest 框架中使用 Tokenauthentication 进行身份验证时,last_login 字段未更新 - last_login field is not updated when authenticating using Tokenauthentication in Django Rest Framework 如何在Django rest框架中对用户进行身份验证? - how to authenticate users in Django rest framework? 如何使用 django rest 框架对用户进行身份验证 - How to authenticate a user using django rest framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM