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()

ただし、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:「モジュール」オブジェクトには属性「セッション」がありません

次に、ページをリロードすると、次のエラーが表示されます。

AttributeError: '_Serving' オブジェクトに属性 'session' がありません

編集:コントローラークラスでこれを試しても、「モジュールオブジェクトには属性セッションがありません」というエラーが表示されます:

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 に答える