【问题标题】:CherryPy access restrictions with static filesCherryPy 对静态文件的访问限制
【发布时间】:2014-06-08 20:35:15
【问题描述】:

我有一个 CherryPy 服务器,可以分发一些静态 HTML/JS/等。文件到 /foosball,加上一些通过 REST API 到 / 的 JSON。

import cherrypy

config = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': # my port here,
        'tools.json_in.on': True
    },
    '/foosball': {
        'tools.staticdir.on': True,
        'tools.staticdir.root': '/var/www/html',
        'tools.staticdir.dir': 'foosball',
        'tools.staticdir.index': 'index.html'
    }
}

@cherrypy.popargs('player_id')
class RESTClient_Player(object):
    # stuff

class RESTClient_Game(object):
    # stuff

class RESTClient:
    players = RESTClient_Player()
    games = RESTClient_Game()

    @cherrypy.expose
    def index(self):
        http_method = getattr(self, cherrypy.request.method)
        return (http_method)()

cherrypy.quickstart(RESTClient(), '/', config)

我还希望通过基本访问限制方案保护这些页面,因此我一直在研究the excellent tutorial CherryPy provides

麻烦的是,文档是针对非静态页面的验证,这种页面由def 声明明确声明。我尝试将此文档改编为 /foosball 中的文件,但没有成功。 /foosball 总是在没有任何身份验证请求的情况下加载。

我可以添加什么来赋予静态文件一些访问限制能力?

谢谢!


编辑:我被指向 auth_tool。使用下面的配置块,我可以使用登录屏幕锁定 REST API 部分,但 /foosball 中的所有静态文件仍然可以公开访问:

def check_login_and_password(login, password):
    cherrypy.log(login)
    cherrypy.log(password)

    return

config = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': # my port here,
        'tools.json_in.on': True,
        'tools.sessions.on': True,
        'tools.session_auth.on': True,
        'tools.session_auth.check_username_and_password': check_login_and_password
    },
    '/foosball': {
        'tools.staticdir.on': True,
        'tools.staticdir.root': '/var/www/html',
        'tools.staticdir.dir': 'foosball',
        'tools.staticdir.index': 'index.html',
        'tools.sessions.on': True,
        'tools.session_auth.on': True,
        'tools.session_auth.check_username_and_password': check_login_and_password
    }
}

【问题讨论】:

  • 试试session_auth工具?
  • 谢谢,我不知道这一点。请参阅上面的编辑了解我的经验。
  • 文件必须来自浏览器缓存,尝试 Ctrl + Refresh。我刚刚尝试过,身份验证工具确实对我有用,尽管在登录表单显示上将内容类型从图像更改为 html 还不够聪明。希望它是可配置的。
  • 之前发布的缓存不是罪魁祸首(我炸毁了一周的浏览器缓存),但是当我在 Internet Explorer(我从未使用过的浏览器)中点击相同的 URL 时,我得到了正确的登录页面。很奇怪。但我想我现在就是我想去的地方,只要我能弄清楚如何正确清除用户的缓存。顺便说一句,@jwalker,如果您将您的评论作为答案重新发布,我会给您信用。
  • 对于缓存控制尝试expires 工具。不确定我的建议是否能得到答案,尤其是因为我们还不知道它是否真的可行。

标签: python cherrypy


【解决方案1】:

您可以在您的类中创建一个返回静态文件的函数,而不是在您的配置中使用“staticdir”。如果这样做,您可以将身份验证包装在您的函数周围。

import cherrypy
from cherrypy.lib.static import serve_file
import os

class Hello(object):
    @cherrypy.expose
    def index(self):
        return "Hello World"

    @cherrypy.expose
    def static(self, page):
        return serve_file(os.path.join(current_dir, 'static', page), content_type='text/html')


if __name__ == '__main__':
    current_dir = os.path.dirname(os.path.abspath(__file__))
    cherrypy.quickstart(Hello())

【讨论】:

    猜你喜欢
    • 2014-08-04
    • 2011-08-06
    • 1970-01-01
    • 2012-04-06
    • 2011-04-26
    • 2015-02-21
    • 2021-09-08
    • 2019-11-17
    相关资源
    最近更新 更多