如何在将密码保存到用户模型 Django 之前对其进行加密?
Posted
技术标签:
【中文标题】如何在将密码保存到用户模型 Django 之前对其进行加密?【英文标题】:How to Encrypt Password before saving it to User Model Django? 【发布时间】:2020-08-16 03:39:21 【问题描述】:我创建了一个用户注册页面,将用户添加到 Django 中的用户模型
但在保存数据时,密码并未加密,即仅以文本形式存储。 这会在用户尝试登录时产生问题(因为 Django 尝试解密原始密码,但它们都不匹配)。
另外,我正在扩展用户模型,以便添加有关保存在配置文件模型中的用户的更多信息(使用一对一链接)
views.py
def user_signup(request):
if request.method == "POST":
user_form = userSignup(request.POST)
phone = request.POST['phone']
address = request.POST['address']
pincode = request.POST['pincode']
if user_form.is_valid() :
user = user_form.save()
auth.login(request,user)
userdata = User.objects.all()
for userinfo in userdata:
if userinfo.username == user.username:
user_id=user.id
update_data = Profile.objects.get(pk = user_id)
update_data.address=address
update_data.phone=phone
update_data.pincode=pincode
update_data.save()
return redirect('/')
else:
return HttpResponse(" SIGNUP FAILED")
else:
form = userSignup()
profile_form = userSignup_profile()
return render(request,"user_signup.html",'form':form, 'profile_form':profile_form)
def user_logout(request):
auth.logout(request)
return redirect('/')
user_signup.html
<body>
<form action="user_signup" method="POST">
% csrf_token %
form.as_p
profile_form.as_p
<button class="primary" type="submit" >SIGNUP </button>
</form>
</body>
Models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=10)
address = models.TextField(max_length=200)
pincode = models.IntegerField()
forms.py
class userSignup(forms.ModelForm):
password = forms.CharField(max_length=50, widget = forms.PasswordInput())
class Meta:
model = User
fields = ('first_name', 'last_name','username', 'password', 'email')
如何以加密形式保存新的用户密码,而不是实际密码??
【问题讨论】:
set_password
【参考方案1】:
import crypt
# To encrypt the password. This creates a password hash with a random salt.
password_hash = crypt.crypt(password)
# To check the password.
valid_password = crypt.crypt(cleartext, password_hash) == password_hash
来源:https://docs.djangoproject.com/en/1.8/topics/auth/passwords/#django.contrib.auth.hashers.make_password
【讨论】:
【参考方案2】:Django make_password (source code) 函数将纯文本密码转换为适合存储在持久数据库中的哈希值。
当这个函数已经存在时,你绝对不想尝试使用你自己的加密和散列函数来存储密码。
只需将您的views.py
编辑为:
from django.contrib.auth.hashers import make_password
def user_signup(request):
if request.method == "POST":
user_form = userSignup(request.POST)
phone = request.POST['phone']
address = request.POST['address']
pincode = request.POST['pincode']
if user_form.is_valid() :
# Hash password using make_password() function
user = user_form.save(commit=False)
user.password = make_password(user.password)
user.save()
...
【讨论】:
它没有用。Exception Type : AttributeError
Exception Value: 'userSignup' object has no attribute 'password'
【参考方案3】:
# You have to import make_password
from django.contrib.auth.hashers import make_password
# you have to pass string as parameter
password = "123"
make_password(password)
# You can write your code like this:-
def user_signup(request):
if request.method == "POST":
user_form = userSignup(request.POST)
phone = request.POST['phone']
address = request.POST['address']
pincode = request.POST['pincode']
if user_form.is_valid() :
user = user_form.save(commit=False)
user.password = make_password("123")
user.save()
.......
.......
【讨论】:
以上是关于如何在将密码保存到用户模型 Django 之前对其进行加密?的主要内容,如果未能解决你的问题,请参考以下文章
使用 SwiftUI、Combine 和 Firebase,我如何在将用户的帐户链接到电子邮件/密码之前验证用户是不是已匿名登录?