现在的位置: 首页 > 综合 > 正文

repoze.what.plugins.quickstart调用流程解析

2013年05月23日 ⁄ 综合 ⁄ 共 1848字 ⁄ 字号 评论关闭
repoze.what.plugins.quickstart调用流程解析
在使用repoze.what.plugins的时候曾碰到这样的情况:
用户A已经登录,此时用户在用户A没有关闭浏览器的情况下(即session没有失效)再次登录,显示登录成功,但登录的用户还是A而不是B,究其原因,应该是cookie中的用户A的信息没有删除。怎样才能不显式调用logout url(如果调用logout会导致页面跳转)而实现清空上一用户信息的效果?为搞清楚这一问题,就有必要探究一下repoze.what.plugins.quickstart的调用次序。
repoze.what.plugins.quickstart setup_sql_auth调用repoze.what.middleware setup_auth 调用repoze.who.plugins.testutil make_middleware,make_middleware根据是否skip_authentication,决定是调用AuthenticationForgerMiddleware还是repoze.who.middleware的PluggableAuthenticationMiddleware,实际使用中调用的应该是PluggableAuthenticationMiddleware
setup_sql_auth中创建cookie
cookie = AuthTktCookiePlugin(cookie_secret, cookie_name,
                                 timeout=cookie_timeout,
                                 reissue_time=cookie_reissue_time)
然后将cookie保存在who_args['identifiers'].append(('cookie', cookie))中。
who_args从repoze.what.plugins.quickstart setup_sql_auth一直传递到repoze.who.middleware的PluggableAuthenticationMiddleware中。
在PluggableAuthenticationMiddleware中通过
iregistry, nregistry = make_registries(identifiers, authenticators,
                                               challengers, mdproviders)
self.name_registry = nregistry
environ['repoze.who.plugins'] = self.name_registry
等操作将setup_sql_auth中创建cookie保存于environ['repoze.who.plugins']['cookie']中
AuthTktCookiePlugin的定义在repoze.who.plugins.auth_tk中,AuthTktCookiePlugin有一方法定义如下:
# IIdentifier
    def forget(self, environ, identity):
        # return a set of expires Set-Cookie headers
        return self._get_cookies(environ, 'INVALID', 0)
调用该forget方法即可将相关cookie清除,从而达到清除上一用户信息的目的。
在pylons中的调用示例如下:
def change_account(self):
        # Change another account, clear the original cookie
        if request.environ['repoze.who.plugins']['cookie']:
            headers = request.environ['repoze.who.plugins']['cookie'].forget(request.environ, None)
            response.headerlist.extend(headers)
        redirect(url('/account/login'))

抱歉!评论已关闭.