用烧瓶保护静态文件 [英] Secure static files with flask

查看:195
本文介绍了用烧瓶保护静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,我希望它只在用户通过身份验证时才能提供一些静态文件。这是一个非常低流量的应用程序(仅供内部使用)。我将如何去做这件事?有一件事我正在考虑的是使用serve_static(),并把它放在一个身份验证检查之后,但是使用了已经提供内容的静态目录。

解决方案

简单地继承 flask.Flask 并覆盖 send_static_file 方法:

  class SecuredStaticFlask(Flask):
def send_static_file(self,filename):
#如果user.is_authenticated():
返回super(SecuredStaticFlask ,self).send_static_file(文件名)
else:
abort(403)
#或者401(或404),无论哪个最适合你的情况

另见 send_static_file 和以下


I am building a flask application, and I want it to serve some static files only if the user is authenticated. It's a very low-traffic application (for internal use only). How would I go about this? One thing I was thinking of is using serve_static(), and putting that behind an authentication check but that uses the static directory which flask already serves content from.

解决方案

Simply subclass flask.Flask and override the send_static_file method:

class SecuredStaticFlask(Flask):
    def send_static_file(self, filename):
        # Get user from session
        if user.is_authenticated():
            return super(SecuredStaticFlask, self).send_static_file(filename)
        else:
            abort(403) 
            # Or 401 (or 404), whatever is most appropriate for your situation

See also the definition of send_static_file and following

这篇关于用烧瓶保护静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆