一文详解Flask-Login
Flask-Login 为 Flask 提供用户会话管理。它处理登录、注销和长时间记住用户会话等常见任务。
Flask-Login 不绑定到任何特定的数据库系统或权限模型。唯一的要求是您的 用户对象实现一些方法,并且您向能够 从用户 ID 加载用户 的扩展提供回调。
GitHub:https://github.com/maxcountryman/flask-login
LoginManager
是一个类,有多个方法和属性;该类初始化的对象用于保存用于登录的设置。LoginManager 实例不绑定到特定应用程序,因此可以在代码的主体中创建一个,然后将其绑定到您的应用程序 app 中工厂函数。
login-view
:验证失败跳转的界面。login-message
:用户重定向到登录页面时闪出的消息。refresh-view
:用户需要重新进行身份验证时要重定向到的视图的名称。needs-refresh-message
:用户重定向到 “需要刷新” 页面时闪出的消息。session-protection
:使用会话保护的模式。这可以是basic
(默认)或strong
,或None
禁用。
classLoginManager:def__init__(self, app=None, add_context_processor=True):#: A class or factory function that produces an anonymous user, which#: is used when no one is logged in.
self.anonymous_user = AnonymousUserMixin
#: The name of the view to redirect to when the user needs to log in.#: (This can be an absolute URL as well, if your authentication#: machinery is external to your application.)
self.login_view =None#: Names of views to redirect to when the user needs to log in,#: per blueprint. If the key value is set to None the value of#: :attr:`login_view` will be used instead.
self.blueprint_login_views ={}#: The message to flash when a user is redirected to the login page.
self.login_message = LOGIN_MESSAGE
#: The message category to flash when a user is redirected to the login#: page.
self.login_message_category = LOGIN_MESSAGE_CATEGORY
#: The name of the view to redirect to when the user needs to#: reauthenticate.
self.refresh_view =None#: The message to flash when a user is redirected to the 'needs#: refresh' page.
self.needs_refresh_message = REFRESH_MESSAGE
#: The message category to flash when a user is redirected to the#: 'needs refresh' page.
self.needs_refresh_message_category = REFRESH_MESSAGE_CATEGORY
#: The mode to use session protection in. This can be either#: ``'basic'`` (the default) or ``'strong'``, or ``None`` to disable#: it.
self.session_protection ="basic"......
user_loader
:自定义回调函数。这将设置从会话重新加载用户的回调。您设置的函数应该使用 用户 ID(“unicode”)并返回用户对象,如果用户不存在则返回 “None”。源码如下:
defuser_loader(self, callback):"""
This sets the callback for reloading a user from the session. The
function you set should take a user ID (a ``str``) and return a
user object, or ``None`` if the user does not exist.
:param callback: The callback for retrieving a user object.
:type callback: callable
"""
self._user_callback = callback
return self.
自定义回调函数。在执行下面这段代码之后,注册了
load_user()
这个自定义的
callback
。
@login_manager.user_loaderdefload_user(userid):return User.get(userid)
utils
login_required
:如果使用此装饰视图,它将确保在调用实际视图之前登录并验证当前用户。如果验证不通过,那么则会调用LoginManager.unauthorized()
。login_user
:记录 / 保存当前成功登陆的用户。logout_user
:登出功能类似,除了基本的操作外,还需要把 flask-login 中的登出进行操作。
UserMixin
:要简便地实现用户类,你可以从
UserMixin
继承,它提供了对下列这些方法的默认实现。(虽然这不是必须的。)
is_authenticated
:当用户通过验证时,也即提供有效证明时返回True
。(只有通过验证的用户会满足login_required
的条件。)is_active
:如果这是一个活动用户且通过验证,账户也已激活,未被停用,也不符合任何你的应用拒绝一个账号的条件,返回True
。不活动的账号可能不会登入(当然, 是在没被强制的情况下)。is_anonymous
:如果是一个匿名用户,返回True
。(真实用户应返回False
。)get_id()
:返回一个能唯一识别用户的,并能用于从user_loader
回调中加载用户的unicode
。注意必须是一个unicode
,如果 ID 原本是一个int
或其它类型,你需要把它转换为unicode
。
Flask-Login 一般使用基础流程
Flask-Login 通过 user session,提供登录的常见任务,比如登入 (logging in)、登出 (logging out) 和当前用户 (current user)。
login_user()
:实现用户的登入,一般在登入的视图函数中调用。
logout_user()
:实现登出功能。
current_user
属性:获取当前用户。
如果需要页面是授权用户才可见,在相应视图函数前加上
@login_required
装饰器进行声明即可,
@login_required
装饰器对于未登录用户访问,默认处理是重定向到
LoginManager.login_view
所指定的视图。
实战
首先,我们将设置一个 Flask 应用程序:
import flask
app = flask.Flask(__name__)
app.secret_key ='super secret string'# Change this!
Flask-Login 通过登录管理器工作。首先,我们将通过实例化登录管理器并告诉它我们的 Flask 应用程序来设置登录管理器:
import flask_login
login_manager = flask_login.LoginManager()# 初始化一个 LoginManager 类对象
login_manager.login_view ='login'
login_manager.login_message_category ='info'
login_manager.login_message ='Access denied.'
login_manager.init_app(app)# 配置该对象
为了简单起见,我们将使用字典来表示用户数据库。在实际应用程序中,这将是一个实际的持久层。然而,重要的是要指出这是 Flask-Login 的一个特性:它不关心你的数据是如何存储的,只要你告诉它如何检索它!
# Our mock database.
users ={'[email protected]':{'password':'secret'}}
我们还需要告诉 Flask-Login 如何从 Flask 请求及其会话中 加载用户。为此,我们需要定义我们的用户对象、一个
user_loader
回调和一个
request_loader
回调。
classUser(flask_login.UserMixin):pass@login_manager.user_loaderdefuser_loader(email):if email notin users:return
user = User()
user.id= email
return user
@login_manager.request_loaderdefrequest_loader(request):
email = request.form.get('email')if email notin users:return
user = User()
user.id= email
return user
现在我们准备定义我们的观点。我们可以从登录视图开始,它将使用身份验证位填充会话。之后我们可以定义一个需要身份验证的视图。
@app.route('/login', methods=['GET','POST'])deflogin():if flask.request.method =='GET':return'''
<form action='login' method='POST'>
<input type='text' name='email' id='email' placeholder='email'/>
<input type='password' name='password' id='password' placeholder='password'/>
<input type='submit' name='submit'/>
</form>
'''
email = flask.request.form['email']if email in users and flask.request.form['password']== users[email]['password']:
user = User()
user.id= email
flask_login.login_user(user)return flask.redirect(flask.url_for('protected'))return'Bad login'@app.route('/protected')@flask_login.login_requireddefprotected():return'Logged in as: '+ flask_login.current_user.id
最后,我们可以定义一个视图来清除会话并将用户注销。
@app.route('/logout')deflogout():
flask_login.logout_user()return'Logged out'
我们现在有了一个基本的工作程序,它使用了基于会话的认证。为了使事情圆满结束,我们应该为登录失败提供一个回调。
@login_manager.unauthorized_handlerdefunauthorized_handler():return'Unauthorized',401
版权归原作者 皮皮要HAPPY 所有, 如有侵权,请联系我们删除。