Python:Flask:为login_required捕获所有URL

时间:2013-05-16 16:28:27

标签: python flask flask-login

我希望用户在没有登录的情况下无法获取任何资产。任何人都可以告诉我为什么以下内容不起作用:

http://domain-name:5000/static/index.html.

即使用户未登录,也会获得index.html文件的服务。

lm.login_view = "/static/login.html"
@app.route('/',defaults={'path':''})
@app.route('/static/<path:path>')
@login_required
def root():
    logging.debug('Login Required - Authenticated user. Will Redirect')
    return redirect(path)

谢谢!

1 个答案:

答案 0 :(得分:1)

默认情况下,如果存在static文件夹,则static端点会将static个网址路径连接到static文件夹路径。您可以将static_url_pathstatic_folder flask参数更改为另一个(不是static)。

如果您想要登录静态端点,那么您可以尝试下一个代码:

@app.before_request
def check_login():
    if request.endpoint == 'static' and not current_user.is_authenticated():
        abort(401)
    return None

或覆盖send_static_file查看功能:

def send_static_file(self, filename):
    if not current_user.is_authenticated():
        abort(401)
    return super(Flask, self).send_static_file(filename)
相关问题