简体   繁体   English

提供 static 文件夹时使用 Flask-HTTPAuth

[英]Using Flask-HTTPAuth when serving a static folder

I'm using Flask to serve a static folder:我正在使用 Flask 为 static 文件夹提供服务:

from flask import Flask, send_from_directory
from flask_httpauth import HTTPBasicAuth

app = Flask(__name__,
            static_url_path='',
            static_folder='html_files')
...

@app.route('/')
@auth.login_required
def send_html_files():
    return send_from_directory('html_files', 'main.html')

I used the first example in Flask-HTTPAuth docs in order to add basic authentication to my website.我使用了Flask-HTTPAuth 文档中的第一个示例,以便为我的网站添加基本身份验证。 Just a regular username and password is enough for me.对我来说,只需一个普通的用户名和密码就足够了。

The problem is that the authentication dialog is not showing when the user go directly to http://localhost:5000/a/b/c (it works on http://localhost:5000/)问题是当用户 go 直接到 http://localhost:5000/a/b/c 时,身份验证对话框没有显示(它适用于 http://localhost:5000/)

What is the proper way of doing this?这样做的正确方法是什么? On the other hand, what is the quick and dirty way?另一方面,又快又脏的方法是什么?

@app.route('/') matches your root path only. @app.route('/')仅匹配您的根路径。

Try something like this to match every path:尝试这样的事情来匹配每条路径:

@app.route('/<path:filename>')
@auth.login_required
def send_html_files(filename):  
    return send_from_directory('html_files', filename)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM