[英]is this way of using current_user from Flask-Login safe?
我无法理解 Flask-Login 库中的 current_user 如何工作。 例如,当两个用户同时访问同一路由并使用其 function 时,我调用多个模块,这些模块也使用 current_user 作为导入。 我将用一些代码详细说明:
我有一条名为 update_account 的路线(我删除了一些部分,因为它们与我的问题无关):
@users.route('/account/user/<username>/update', methods=['GET', 'POST'])
def update_account(username):
update_account_form = UpdateForm()
if update_account_form.validate_on_submit():
#here we handle updating from another module
if AccountManager.update_account(update_account_form): #retuns True if no errors has occured
flash('Your account has been successfully updated', "success")
return redirect(url_for('users.update_account', username=current_user.username))
flash('Your client matched max requests', "warning")
return redirect(url_for('users.update_account', username=current_user.username))
return render_template('account/update.html', update_form=update_account_form)
我的问题是关于我调用AccountManager.update_account(update_account_form)
的部分,因为我没有传递任何 current_users 数据,而是在该模块中导入 current_user,这就是我获取数据的方式。 以下是我如何实现的:
from flask_login import login_user, current_user
class AccountManager:
@staticmethod
def update_account(account_form):
if current_user.request_counter >= 5:
return False
current_user.username = account_form.username.data.strip()
current_user.email = account_form.email.data.strip()
if account_form.change_password.data:
current_user.password = bcrypt.generate_password_hash(account_form.password.data).decode('utf-8')
db.session.commit()
return True
我的问题就在这里。 这安全吗? 我应该将 current_user 作为参数传递而不是在此处导入吗? 因为也许如果另一个请求来了 current_user 改变,这个方法会改变其他人的数据。
谢谢你的时间。
你在做什么很好。
current_user
取决于请求上下文,因此您不会仅仅因为另一个用户的请求在您完成第一个处理之前进入而获得不同的用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.