编程语言
首页 > 编程语言> > 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,app,BasicAuth,Flask,secret,BASIC
来源: https://blog.51cto.com/u_13567403/3041686