我想在用户登录时进行一些自定义。问题是该项目正在使用flask-security,它隐式处理用户登录。
我想在用户登录时检查数据库中用户的一些记录。
如何覆盖烧瓶安全中的“登录”功能?
I saw a 类似的帖子 https://stackoverflow.com/questions/25114445/how-to-run-custom-code-at-login-with-flask-security,并尝试过,但不起作用。
另外,这并不完全是我想做的。
对于某些用户,我可能需要停止默认行为。
那么,有人遇到这样的问题吗?我该怎么做?
您可以在设置 Flask-Security 时覆盖登录表单:
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore, login_form=CustomLoginForm)
然后创建扩展默认 LoginForm 的自定义登录表单类。并覆盖验证函数以在登录尝试之前或之后执行操作。
from flask_security.forms import LoginForm
class CustomLoginForm(LoginForm):
def validate(self):
# Put code here if you want to do stuff before login attempt
response = super(CustomLoginForm, self).validate()
# Put code here if you want to do stuff after login attempt
return response
根据登录是否成功,“reponse”将为 True 或 False。如果您想检查登录尝试期间可能发生的错误,请参阅“self.errors”
问候,
凯文;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)