<p>你说得对,<code>create_user()</code>不会散列密码。它是一个<a href="https://github.com/mattupstate/flask-security/issues/136#issuecomment-21195163" rel="nofollow noreferrer">lower-level method</a>。如果您能够使用<code>registerable.register_user()</code>,那么它将为您散列密码。但是如果您想直接使用<code>create_user()</code>,那么只需在调用密码之前对其进行加密:</p>
<pre><code>from flask import request
from flask_security.utils import encrypt_password
@bp.route('/register/', methods=['GET', 'POST'])
@anonymous_user_required
def register():
form = ExtendedRegistrationForm(request.form)
if form.validate_on_submit():
form_data = form.to_dict()
form_data['password'] = encrypt_password(form_data['password'])
user = security.datastore.create_user(**form_data)
security.datastore.commit()
# etc.
</code></pre>
<p>我不建议重写User对象上的密码哈希,因为Flask Security使用<code>SECURITY_PASSWORD_HASH</code>设置来存储密码哈希算法。(它是<a href="https://pythonhosted.org/Flask-Security/configuration.html?highlight=security_password_hash" rel="nofollow noreferrer">defaults</a>到{<cd5>},所以如果不想的话,您不需要显式地设置它。)Flask Security <a href="https://stackoverflow.com/a/55013458/1749551">uses HMAC to salt the password</a>,除了您提供的<code>SECURITY_PASSWORD_SALT</code>之外,只需使用passlib和bcrypt<a href="https://github.com/mattupstate/flask-security/issues/816" rel="nofollow noreferrer">won't result in a hash that Flask-Security will correctly match</a>来散列密码。你也许可以绕过这一步,把烧瓶的安全性从循环中去掉,自己去做所有的密码创建和比较任务……但这有什么意义呢?你在使用一个安全库,让它为你做安全保护。他们已经修复了你一定会遇到的错误。在</p>