gpt4 book ai didi

python - 不在 Flask 中散列密码

转载 作者:可可西里 更新时间:2023-11-01 09:24:28 25 4
gpt4 key购买 nike

我不知道为什么密码没有使用 Bcrypt 进行哈希处理。我认为我做对了。我正确地初始化了 Bcrypt,并且我正在使用 mongoengine。每次我查看数据库时,它仍然以文本形式显示未加密的密码。

用户/模型.py

from app import db, bcrypt
class User(db.Document):

username = db.StringField(required=True)
first_name = db.StringField(required=True)
last_name = db.StringField(required=True)
email = db.EmailField(required=True)
password = db.StringField(required=True)

meta = {'collection': 'users'}

@property
def hash_password(self):
return self.password

@hash_password.setter
def set_hash_password(self, password):
self.password = bcrypt.generate_password_hash(password)

def verify_password(self, password):
return bcrypt.check_password_hash(self.password, password)

用户/views.py

@userV.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignUpForm()

if form.validate_on_submit():
user = User(
first_name=form.first_name.data,
last_name=form.last_name.data,
username=form.username.data,
email=form.email.data,
password=form.password.data
).save()

flash('You can now login')
return render_template('user.html', variable="You can now login " + user.username)

return render_template('signup.html', form=form)

用户/授权/forms.py

class SignUpForm(Form):
username = StringField('Username', validators=[
InputRequired(message="Username is required"),
Regexp('^[A-Za-z][A-Za-z0-9_]*[A-Za-z0-9]$', 0, 'Usernames must have only letters, numbers or underscores')
])
first_name = StringField('First Name', validators=[
InputRequired(message="First name is required")
])
last_name = StringField('Last Name', validators=[
InputRequired(message="Last name is required")
])
email = StringField('Email Address', validators=[
InputRequired(message="Email is required"),
Email(message="This is not a valid email")
])
password = PasswordField('Password', validators=[
InputRequired(message="Password is required"),
Length(min=6, message="The password is not long enough")
])
accept_tos = BooleanField('Accept Terms of Service', validators=[
InputRequired(message="You have to accept the Terms of Service in order to use this site")
])
submit = SubmitField('Signup')

def validate(self):
if not Form.validate(self):
return False

if User.objects(username=self.username.data).first():
raise ValidationError('Username already in use')

if User.objects(email=self.email.data).first():
raise ValidationError('Email already registered')

return True

这是我搜索 mongodb shell 时的结果。密码未经过哈希处理。

{ "_id" : ObjectId("555df97deddd5543c360888a"), "username" : "FullMetal", "first_name" : "Edward", "last_name" : "Elric", "email" : "fullmetalalchemist@gmail.com", "password" : "equalexchange" }

最佳答案

该属性称为 hash_password 而不是 password。我没有看到 hash_password 在哪里被分配(那是它的 setter 被调用的时候)。此外,您的 setter 方法应与属性本身具有完全相同的名称,在本例中为 hash_password 而不是 (set_hash_password)。然后你可以做

user = User(hash_password=form.password.data)

不幸的是,由于 mongoengine.Document.__init__ 的工作方式,您将无法以这种方式使用您的字段。您有两种选择来使其工作:

方案一:先创建没有密码的User对象,然后设置hash_password,然后保存

user = User(first_name=form.first_name.data,
last_name=form.last_name.data,
username=form.username.data,
email=form.email.data)
user.hash_password = form.password.data
user.save()

选项 2:需要覆盖 User__init__ 方法

class User(db.Document):
def __init__(self, *args, **kwargs):
if 'hash_password' in kwargs:
self.hash_password = kwargs.pop('hash_password')
super(User, self).__init__(*args, **kwargs)

现在您可以按照最初的意愿使用 User:

user = User(first_name=form.first_name.data, hash_password=form.password.data)

关于python - 不在 Flask 中散列密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com