Skip to main content
 首页 » 编程设计

python之Pyramid Web 应用程序中的( session )身份验证

2025年05月04日12emanlee

我正在开发一个基于 Pyramid 框架的 Python Web 应用程序。我正在尝试向其添加 session 身份验证。我的理解是:

  • 用户可以登录/注销(安全是可取的);用户数据保存在数据库中
  • 身份验证通过 session 处理 (request.session)

首先: session 身份验证是一个好的选择还是有更好的选择? 其次:我真的无法理解文档和示例。

到目前为止,我已经关注了 http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/tutorials/wiki2/authorization.html#adding-login-and-logout-views到目前为止,我有一个登录/注销表单。但是,我的 authn_policy 是 http://docs.pylonsproject.org/projects/pyramid/en/latest/api/authentication.html#pyramid.authentication.SessionAuthenticationPolicy

由于 Pyramid 中的 session 工厂是不安全的(参见 http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/sessions.html ),我改用 *pyramid_beaker*。

配置是: 在 __init__.py 中:session_factory = session_factory_from_settings(settings)

在 .ini 文件中:

beaker.session.lock_dir = %(here)s/data/sessions/lock 
beaker.session.type = ext:database 
beaker.session.sa.url = mysql://user:pass@localhost:3306/db 
beaker.session.table_name = user_session 

我希望我能把我的问题说清楚。

请您参考如下方法:

我会说这取决于你想做什么。如果您使用 Beaker, session 身份验证工作正常,但我喜欢使用 AuthTktAuthenticationPolicy 来获得额外的超时和重发选项,而且即使您清除 session ,您的身份验证也不会消失。