Python教程

Python编程:Flask-BasicAuth实现Authentication登录认证

本文主要是介绍Python编程:Flask-BasicAuth实现Authentication登录认证,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

https://flask-basicauth.readthedocs.io/en/latest/

通过Flask-BasicAuth,提供用户名密码即可实现Authentication(认证)机制

安装

pip install Flask-BasicAuth

代码示例

# -*- coding: utf-8 -*-

# @Date    : 2018-10-18
# @Author  : Peng Shiyu


from flask import Flask
from flask_basicauth import BasicAuth

app = Flask(__name__)

app.config['BASIC_AUTH_USERNAME'] = 'admin'
app.config['BASIC_AUTH_PASSWORD'] = '123456'

# app.config['BASIC_AUTH_FORCE'] = True  # 整个站点都验证

basic_auth = BasicAuth(app)


@app.route('/')
def index():
    return "hello world"


@app.route('/secret')
@basic_auth.required
def secret():
    return "hello world secret"


if __name__ == '__main__':
    app.run()

访问网站的时候,会弹框要求登录验证

在这里插入图片描述

参考
Flask-BasicAuth

这篇关于Python编程:Flask-BasicAuth实现Authentication登录认证的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!