django 无需密码进行身份验证
我使用 django 的默认身份验证系统,但我添加了 OpenID 库,我可以在其中通过 OpenID 对用户进行身份验证。我想做的是让他们登录,但似乎使用默认的 django 身份验证系统,我需要他们的密码来验证用户身份。有没有办法在不实际使用密码的情况下解决这个问题?
我想做这样的事情...
user = ... # queried the user based on the OpenID response
user = authenticate(user) # function actually requires a username and password
login(user)
我很快就放弃了身份验证功能,但它附加了登录所需的后端字段。
I'm using the default authentication system with django, but I've added on an OpenID library, where I can authenticate users via OpenID. What I'd like to do is log them in, but it seems using the default django auth system, I need their password to authenticate the user. Is there a way to get around this without actually using their password?
I'd like to do something like this...
user = ... # queried the user based on the OpenID response
user = authenticate(user) # function actually requires a username and password
login(user)
I sooner just leave off the authenticate
function, but it attaches a backend
field, which is required by login.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此编写自定义身份验证后端很简单。如果您使用以下内容创建 yourapp/auth_backend.py:
然后添加到您的settings.py:
在您看来,您现在可以在没有密码的情况下调用authenticate:
It's straightforward to write a custom authentication backend for this. If you create yourapp/auth_backend.py with the following contents:
Then add to your settings.py:
In your view, you can now call authenticate without a password:
这是一个有点黑客,但如果你不想重写一堆东西,删除身份验证
用户将是你的 User 对象
This is a bit of a hack but if you don't want to rewrite a bunch of stuff remove the authenticate
user would be your User object
为了在没有密码的情况下进行身份验证,请在您的
settings.py
中:在您的
auth_backend.py
中:导入自定义登录请求的视图:
如需进一步参考,请使用 django 文档此处。
In order to do authenticate without password, in your
settings.py
:In your
auth_backend.py
:In your Views for custom login request:
For further reference, use the django documentation here.
您可以通过创建自己的身份验证后端< /a> 并将其添加到
AUTHENTICATION_BACKENDS
设置。已经有一些可用的 OpenID 后端,因此通过一些搜索,您可以省去编写后端的麻烦。
You can easily fix this by creating your own authentication backend and adding it to the
AUTHENTICATION_BACKENDS
setting.There are some OpenID backends available already, so with a bit of searching you could save yourself the trouble of writing one.