为了账号安全,请及时绑定邮箱和手机立即绑定

没有密码的身份验证 Django

没有密码的身份验证 Django

幕布斯6054654 2022-11-09 17:06:21
正如标题所述,我正在尝试验证没有密码的用户。我已经使用过这个:django authentication without a password来解决我的应用程序(在 Django 2.0 上)的问题,但我想在另一个应用程序中做同样的事情,但它在 Django 2.1 上。当我执行相同的实现时,我的自定义身份验证功能永远不会被调用。因此它不起作用。auth_backend.py 中的当前设置:from django.contrib.auth.backends import ModelBackendfrom django.contrib.auth.models import Userclass PasswordlessAuthBackend(ModelBackend):    """Log in to Django without providing a password.    """    def authenticate(self, username=None):        try:            return User.objects.get(username=username)        except User.DoesNotExist:            return None    def get_user(self, user_id):        try:            return User.objects.get(pk=user_id)        except User.DoesNotExist:            return None在 settings.py 中设置:AUTHENTICATION_BACKENDS = [# auth_backend.py implementing Class PasswordlessAuthBackend inside yourapp folder    'yourapp.auth_backend.PasswordlessAuthBackend', # Default authentication of Django    'django.contrib.auth.backends.ModelBackend',]但是当我尝试我的观点时user = authenticate(username=user.username)它永远不会达到我的自定义身份验证方法。任何和所有的帮助表示赞赏!
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

所以我通过这里的文件解决了我自己的问题:https ://docs.djangoproject.com/en/2.1/topics/auth/customizing/

我所要做的就是 auth_backend.py 中的 authetnicate 函数

def authenticate(self, username=None):

def authenticate(self, request, username=None):

在文档中它说您还可以将类声明更改为不包含 ModelBackend,但它可以以任何方式工作。


查看完整回答
反对 回复 2022-11-09
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

您可以尝试避免使用默认后端吗?


改变


AUTHENTICATION_BACKENDS = [

# auth_backend.py implementing Class PasswordlessAuthBackend inside yourapp folder

    'yourapp.auth_backend.PasswordlessAuthBackend', 

# Default authentication of Django

    'django.contrib.auth.backends.ModelBackend',

]


AUTHENTICATION_BACKENDS = [

# auth_backend.py implementing Class PasswordlessAuthBackend inside yourapp folder

    'yourapp.auth_backend.PasswordlessAuthBackend', 

]


查看完整回答
反对 回复 2022-11-09
?
FFIVE

TA贡献1797条经验 获得超6个赞

您在 settings.py 中的身份验证后端路径无效

yourapp.auth_backend.YourAuth

应该

yourapp.auth_backend.PasswordlessAuthBackend


查看完整回答
反对 回复 2022-11-09
  • 3 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号