【Python开发】一文详解Flask-Login
创始人
2024-02-23 10:17:45
0

一文详解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 禁用。
class LoginManager: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”。源码如下:

def user_loader(self, callback):"""This sets the callback for reloading a user from the session. Thefunction you set should take a user ID (a ``str``) and return auser object, or ``None`` if the user does not exist.:param callback: The callback for retrieving a user object.:type callback: callable"""self._user_callback = callbackreturn self.

自定义回调函数。在执行下面这段代码之后,注册了 load_user() 这个自定义的 callback

@login_manager.user_loader
def load_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 flaskapp = flask.Flask(__name__)
app.secret_key = 'super secret string'  # Change this!

Flask-Login 通过登录管理器工作。首先,我们将通过实例化登录管理器并告诉它我们的 Flask 应用程序来设置登录管理器:

import flask_loginlogin_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 = {'foo@bar.tld': {'password': 'secret'}}

我们还需要告诉 Flask-Login 如何从 Flask 请求及其会话中 加载用户。为此,我们需要定义我们的用户对象、一个 user_loader 回调和一个 request_loader 回调。

class User(flask_login.UserMixin):pass@login_manager.user_loader
def user_loader(email):if email not in users:returnuser = User()user.id = emailreturn user@login_manager.request_loader
def request_loader(request):email = request.form.get('email')if email not in users:returnuser = User()user.id = emailreturn user

现在我们准备定义我们的观点。我们可以从登录视图开始,它将使用身份验证位填充会话。之后我们可以定义一个需要身份验证的视图。

@app.route('/login', methods=['GET', 'POST'])
def login():if flask.request.method == 'GET':return '''
'''email = flask.request.form['email']if email in users and flask.request.form['password'] == users[email]['password']:user = User()user.id = emailflask_login.login_user(user)return flask.redirect(flask.url_for('protected'))return 'Bad login'@app.route('/protected') @flask_login.login_required def protected():return 'Logged in as: ' + flask_login.current_user.id

最后,我们可以定义一个视图来清除会话并将用户注销。

@app.route('/logout')
def logout():flask_login.logout_user()return 'Logged out'

我们现在有了一个基本的工作程序,它使用了基于会话的认证。为了使事情圆满结束,我们应该为登录失败提供一个回调。

@login_manager.unauthorized_handler
def unauthorized_handler():return 'Unauthorized', 401

相关内容

热门资讯

全国人大常委会关于《中华人民共... 全国人民代表大会常务委员会关于 《中华人民共和国刑事诉讼法》 第二百九十二条的解释 (2025年12...
渊亭信息科技申请基于检索增强生... 国家知识产权局信息显示,厦门渊亭信息科技有限公司申请一项名为“基于检索增强生成的智能政策问答方法、系...
政策纾困与转型升级并举,中国乳... 乳制品行业是一二三产业深度融合的重要行业。近日,商务部一则公告引发外界对于这一行业发展形势的关注。 ...
重庆荣豪律师事务所:医疗纠纷处... 推荐指数:★★★★★ 在医疗纠纷频发的当下,如何高效、专业地处理医疗纠纷成为众多患者及其家属、医疗机...
(图表)三部重要法律案将提请2... 新华社图表,北京,2025年12月27日 十四届全国人大常委会第十九次会议12月27日表决通过相关...
《财经智库》总裁张燕冬:坚定的... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院共同承...
无证售烟:不起诉,亦须受罚! “未办理烟草专卖零售许可证销售卷烟,为何未追究刑事责任但是被行政处罚?”面对涉案商户的疑问,仁怀市人...
尚福林:要完善长期资本投早、投... 中国财富管理50人论坛理事长、原银监会主席尚福林12月27日在中国财富管理50人论坛2025年会(十...
原创 美... 网友私信:近期火热的美国斩杀线事件,南生怎么禁声了呢?原因很简单,这个问题在2023年4月份那篇介绍...
纽约商品交易所理事会主席Wil... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院承办的...