[英]CherryPy Custom Tool for user authentication
我正在嘗試在我的 CherryPy controller 類中設置一種簡單的裝飾方法,以便用戶在尚未經過身份驗證的情況下被重定向到登錄頁面。 我打算做一個基本的 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()
但是,當我嘗試啟動 web 站點時出現此錯誤:
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:“模塊”object 沒有屬性“會話”
然后當我重新加載頁面時,我得到這個錯誤:
AttributeError: '_Serving' object 沒有屬性 'session'
編輯:即使在我的 controller class 中嘗試了這么多,我仍然得到“模塊 object 沒有屬性會話”錯誤:
class Root(BaseModule):
_cp_config = {'tools.sessions.on': True}
sess = cherrypy.session # Error here
...
我用錯了鈎子。 改變:
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
}, ...
})
然后,在我的 controller 上使用受限方法:
@cherrypy.expose
@cherrypy.tools.authenticate()
def home(self, **kwargs):
...
很可能沒有啟用會話。 session wiki 頁面上有一個示例配置文件,或查看教程 #7 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.