如何从auth_user django python中选择用户给定的用户名和哈希密码

[英]how to select user from auth_user django python for given username and hashed password


i am creating a python api that will return data from mysql database if the email and password are found in the database but the password is hashed, is their any way that i could hash the password entered from the api to compare it to the mysql password.

我正在创建一个python api,它将从mysql数据库返回数据,如果在数据库中找到了电子邮件和密码但是密码是哈希的,那么他们是否可以将从api输入的密码哈希以将其与mysql密码进行比较。

i am using django.contrib.auth for creating new users that automatically created the table auth_user in the database containing the hashed password.

我正在使用django.contrib.auth创建新用户,该用户在包含散列密码的数据库中自动创建表auth_user。

i know one solution is to store the unencrypted password in another table but can it be done without creating another password field?

我知道一个解决方案是将未加密的密码存储在另一个表中但是可以在不创建另一个密码字段的情况下完成吗?

i searched a lot and i couldn't find any solution.

我搜索了很多,我找不到任何解决方案。

2 个解决方案

#1


1  

You can use authenticate. There is no need of hashing the password by yourself, Django already do that. You just have to pass the username and password to authenticate function and it will return you the corresponding User object.

您可以使用身份验证。没有必要自己散列密码,Django已经这样​​做了。您只需传递用户名和密码来验证功能,它将返回相应的User对象。

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(request, username=username, password=password)

#2


0  

You can also use check_password. Every user in django has this method. It will automatically hash it and compare it with the one saved before:

您也可以使用check_password。 django中的每个用户都有这种方法。它将自动哈希并将其与之前保存的值进行比较:

user.check_password(raw_password)

Hope it helps!

希望能帮助到你!

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/09/03/b9f5e2df41d40fe3db3d02f9ff39bd68.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告