这种从 Flask-Login 使用 current_user 的方式安全吗?
is this way of using current_user from Flask-Login safe?
我无法理解 Flask-Login 库中的 current_user 是如何工作的。例如,当两个用户同时访问同一路由并使用其功能时,我调用了多个模块,这些模块也使用 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
取决于请求上下文,因此您不会仅仅因为另一个用户的请求在您处理完第一个请求之前就收到了不同的用户。
我无法理解 Flask-Login 库中的 current_user 是如何工作的。例如,当两个用户同时访问同一路由并使用其功能时,我调用了多个模块,这些模块也使用 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
取决于请求上下文,因此您不会仅仅因为另一个用户的请求在您处理完第一个请求之前就收到了不同的用户。