【问题标题】:Authentication in pyramid金字塔中的身份验证
【发布时间】:2015-04-10 12:59:09
【问题描述】:

我正在尝试在金字塔 (1.4a1) 中设置基本导航。根据tutorial 给出的教程,groupfinder 会在我们记住登录成功后调用。这适用于我的本地,但是当我在服务器上尝试相同的操作时,它根本不会调用 groupfinder,而是在两条路由之间循环。这是我的代码 sn-p:

from pyramid.security import remember, forget, authenticated_userid
from pyramid.httpexceptions import HTTPFound, HTTPForbidden
from pyramid.threadlocal import get_current_registry
from pyramid.url import route_url
from pyramid.view import view_config, forbidden_view_config


@view_config(route_name='index',
    renderer='templates:templates/index.pt',
    permission='Authenticated')
def index_view(request):
    try:
        full_name = (request.user.first_name + ' ' + request.user.last_name)
    except:
        full_name = "Anonymous"
    return {"label": label, "user_name": full_name}

@forbidden_view_config()
def forbidden(request):

    if authenticated_userid(request):
        return HTTPForbidden()

    loc = request.route_url('login.view', _query=(('next', request.path),))
    return HTTPFound(location=loc)

@view_config(route_name='login.view')
def login_view(request):

    came_from =  request.route_url('index')

    #perform some authentication
    username = 'xyz'
    if authenticate(username):
        headers = remember(request, username)
        #user was authenticated. Must call groupfinder internally and set principal as authenticated.
        return HTTPFound(location=came_from, headers=headers)
    else:
        return HTTPForbidden('Could not authenticate.')

    return HTTPForbidden('Could not authenticate.')

另外,我的 ACL 看起来像: __acl__ = [(Allow, Authenticated, 'Authenticated'), DENY_ALL].

有人可以告诉我为什么没有调用 groupfinder 吗?请求路由是否正确发生?此外,相同的代码适用于我的本地设置。所以groupfinder或者ACL授权设置没有问题。

非常感谢!

【问题讨论】:

  • 本地设置和服务器上运行的配置 ini 有什么不同吗?
  • 没有。完全一样。

标签: python-2.7 pyramid


【解决方案1】:

经过大量调试和挖掘,我发现问题非常简单。不知道这种行为的原因,但我在调用AuthTktAuthenticationPolicy() 时添加了secure = True 属性。当我删除这个属性时,它就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2013-03-28
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多