【问题标题】:cherrypy 'tools.sessions.secure' seems to be breaking sessionscherrypy 'tools.sessions.secure' 似乎正在中断会话
【发布时间】:2016-11-01 13:11:10
【问题描述】:

我正在 localhost 上开发一个cherrypy 应用程序,并写了这个来弄清楚会话发生了什么。

import cherrypy

class WhyNotSessions(object):

    @cherrypy.expose
    def index(self):
        if 'count' not in cherrypy.session:
            cherrypy.session['count'] = 0
        cherrypy.session['count'] += 1
        return "Session count is %s" % cherrypy.session.get('count')

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.sessions.secure': True
        }
    }
    cherrypy.quickstart(WhyNotSessions(), '/', conf)

这按预期工作,count 在重新加载时递增 - 只要我从 conf 注释掉 'tools.sessions.secure': True。我想更好地了解这里发生了什么,因为我打算在生产中使用安全会话。

【问题讨论】:

    标签: python session cherrypy


    【解决方案1】:

    我刚刚偶然发现了同样的问题。

    这是因为将“tools.sessions.secure”设置为 True 会将“安全”标志添加到生成的存储会话 ID 的 cookie。

    如果您在 CherryPy 中未使用 HTTPS,则此 cookie 将永远不会在任何后续请求中返回,因此每次都会生成一个新的 session id。

    在 CherryPy 中启用 HTTPS 可以解决此问题。请参阅CherryPy documentation on SSL 了解如何开启此功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2011-09-12
      • 2019-08-26
      • 1970-01-01
      相关资源
      最近更新 更多