Python Flask-Login实现用户会话管理

作者:皮皮要HAPPY 时间:2023-06-05 13:13:50 

Flask-Login 为 Flask 提供用户会话管理。它处理登录、注销和长时间记住用户会话等常见任务。

Flask-Login 不绑定到任何特定的数据库系统或权限模型。唯一的要求是您的 用户对象实现一些方法,并且您向能够 从用户 ID 加载用户 的扩展提供回调。

GitHub:https://github.com/maxcountryman/flask-login

Python 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. 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_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 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 = {'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:
       return
   user = User()
   user.id = email
   return user
@login_manager.request_loader
def request_loader(request):
   email = request.form.get('email')
   if email not in users:
       return
   user = User()
   user.id = email
   return user

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

@app.route('/login', methods=['GET', 'POST'])
def login():
   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_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

来源:https://blog.csdn.net/be_racle/article/details/128081618

标签:Python,Flask,Login,会话管理
0
投稿

猜你喜欢

  • Python3实现的反转单链表算法示例

    2021-09-22 01:33:30
  • Python 如何实现批量转换视频音频的采样率

    2022-06-30 11:16:46
  • python进度条库tqdm的基本操作方法

    2021-03-02 08:31:22
  • 在ASP.NET 2.0中操作数据之四十四:DataList和Repeater数据排序(三)

    2023-07-02 04:45:57
  • Golang 分割字符串的实现示例

    2024-02-23 03:26:10
  • php中Ctype函数用法详解

    2023-06-19 01:43:13
  • python numpy 常用随机数的产生方法的实现

    2021-06-14 22:44:05
  • Golang汇编之控制流深入分析讲解

    2024-05-08 10:15:11
  • xhtml+css页面制作过程中问题的解决方案

    2008-08-05 18:00:00
  • python基础教程之udp端口扫描

    2022-01-10 01:03:22
  • MySQL通透详解架构设计

    2024-01-27 19:00:51
  • 创建mysql表分区的方法

    2024-01-16 11:48:37
  • Python自定义一个异常类的方法

    2022-04-23 12:51:11
  • python glom模块的使用简介

    2021-08-21 10:22:02
  • Python中print()函数的用法详情

    2021-01-11 21:35:17
  • 如何在一个广告旗帜里轮番显示时间长度不一的不同广告?

    2010-06-26 12:35:00
  • python3人脸识别的两种方法

    2021-07-09 23:12:52
  • 如何使用VSCode愉快的写Python于调试配置步骤

    2021-11-21 06:16:12
  • Python实现PS滤镜中的USM锐化效果

    2023-07-10 12:58:24
  • 以实例全面讲解PHP中多进程编程的相关函数的使用

    2023-10-18 12:21:55
  • asp之家 网络编程 m.aspxhome.com