用烧瓶保护静态文件 [英] Secure static files with flask
本文介绍了用烧瓶保护静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
简单地继承 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屋!
查看全文