4

我正在尝试在我的 CherryPy 控制器类中设置一种简单的装饰方法,以便用户在尚未经过身份验证的情况下被重定向到登录页面。我打算做一个基本的 Python 装饰器,但这里的答案建议我改用 CherryPy 自定义工具。所以我正在尝试这样做,但我无法让它发挥作用。这是我所拥有的:

def authenticate():
    user = cherrypy.session.get('user', None)
    if not user:
        raise cherrypy.HTTPRedirect('/?errMsg=Please%20log%20in%20first')

cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate)

/home页面是一个应该仅限于经过身份验证的用户的页面,所以我有这个:

@cherrypy.expose
@cherrypy.tools.authenticate
def home(self, **kwargs):
    tmpl = TemplateDir.get_template('home.mako')
    return tmpl.render()

但是,当我尝试启动我的网站时出现此错误:

Traceback (most recent call last):
  File ".\example.py", line 3, in <module>
    from controller.main import Root
  File "C:\...\controller\main.py", line 9, in <module>
    class Root(BaseModule):
  File "C:\...\controller\main.py", line 19, in Root
    @cherrypy.tools.authenticate
  File "C:\Python26\lib\site-packages\cherrypy\_cptools.py", line 119, in
   __call__ % self._name)
TypeError: The 'authenticate' Tool does not accept positional arguments; you must
  use keyword arguments.

编辑:好的,如果我将自定义工具的使用更改为带括号,我会得到一个不同的错误。

@cherrypy.expose
@cherrypy.tools.authenticate() # Magic parentheses...
def home(self, **kwargs):
    ...

现在我得到:

Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 625, in respond
    self.hooks.run('on_start_resource')
  File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 97, in run
    hook()
  File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 57, in __call__
    return self.callback(**self.kwargs)
  File ".\example.py", line 40, in authenticate
    user = cherrypy.session.get('user', None)
AttributeError: 'module' object has no attribute 'session'

编辑:我打开了会话:

cherrypy.tools.sessions.storage_type = 'file'
cherrypy.tools.sessions.storage_path = r'%s\sessions' % curDir
cherrypy.tools.sessions.timeout = 60
cherrypy.tree.mount(Root(), "/", config={
    '/static': {
        'tools.staticdir.on':True,
        'tools.staticdir.dir':r'%s\static' % curDir,
    },
    '/': {
        'tools.sessions.on':True,
    }
})

当我第一次在 web 方法上使用自定义工具装饰器加载页面时,我收到此错误:

AttributeError:“模块”对象没有属性“会话”

然后当我重新加载页面时,我得到这个错误:

AttributeError:“_Serving”对象没有属性“会话”

编辑:即使在我的控制器类中尝试了这么多,我仍然得到“模块对象没有属性会话”错误:

class Root(BaseModule):
    _cp_config = {'tools.sessions.on': True}
    sess = cherrypy.session # Error here
    ...
4

2 回答 2

5

我用错了钩子。改变:

cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate)

到:

cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)

修复了问题。显然我的authenticate方法在会话打开之前就被调用了,所以它无法访问cherrypy.session. 我的控制器中不需要任何会话开启的东西;我的服务器启动脚本中的所有必要内容如下:

def authenticate():
    ...
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
cherrypy.tree.mount(Root(), "/", config={
    "/": {
        'tools.sessions.on':True,
        'tools.sessions.storage_type':'file',
        'tools.sessions.storage_path':r'%s\sessions' % curDir,
        'tools.sessions.timeout':60
    }, ...
})

然后,在我的控制器中使用受限方法:

@cherrypy.expose
@cherrypy.tools.authenticate()
def home(self, **kwargs):
    ...
于 2011-07-01T20:59:58.207 回答
0

很可能没有启用会话。会话 wiki 页面上有一个示例配置文件,或查看教程 #7

于 2011-07-01T19:45:08.017 回答