flask中对数据的加密使用generate_password_hash
和check_password_hash
注册时的加密
@user_blueprint.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('register.html')
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if not all([username, password]):
return render_template('register.html')
# 保存注册信息
user = User()
user.username = username
user.password = generate_password_hash(password)
db.session.add(user)
db.session.commit()
return redirect(url_for('user.login'))
登录时的解密
@user_blueprint.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if not all([username, password]):
return render_template('login.html')
user = User.query.filter(User.username == username).first()
if user and check_password_hash(user.password,password):
session['login_status'] = 1
return redirect(url_for('user.index'))
else:
return render_template('login.html')
网友评论