2017-07-04 94 views
2

我想在用户登录时进行一些自定义。问题是该项目正在使用flask-security,它隐式处理用户登录。 我想在用户登录时检查数据库中的用户记录。如何覆盖flask-security的登录?

我如何重写flask安全中的“登录”功能?

我看到similar post,试过但不工作。另外,这不完全是我想要做的。 我可能需要在某些用户的情况下停止默认行为。

那么,有这种问题的人呢?我该怎么做?

谢谢!

回答

3

您可以覆盖登录表单,当你设置的烧瓶安全:

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”

问候, Kevin;)