在Heroku的Sinatra应用程序中,会话不是跨Dynos共享的 [英] In a Sinatra App on Heroku, Session Is Not Shared Across Dynos

查看:91
本文介绍了在Heroku的Sinatra应用程序中,会话不是跨Dynos共享的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有道理的。但是,这个问题有哪些首选的解决方法?在解决方案中,我建议使用基于cookie的会话记录,但看着它,Sinatra会话 仍然是Rack cookie会话。

展望未来,我在Sinatra文档中发现了这一点
$ b


为了提高安全性,cookie中的会话数据使用会话密钥进行签名。 Sinatra为您生成一个随机秘密。但是,由于这个秘密会随着应用程序的每一次启动而改变,因此您可能需要自己设置秘密,以便所有应用程序实例都共享它:




$ b set:session_secret,'super secret' $ b

因此,似乎每个Heroku dyno都生成不同的密钥,因此不能读取其他每个会话cookie,并且您需要指定一个密钥,以便每个dyno使用相同的密钥。



与其向您的源代码添加密钥,您可能更好地设置环境变量

  $ heroku config:add SESSION_KEY = a_longish_secret_key 



然后在你的sinatra应用程序中:

  enable: session 
set:session_secret,ENV ['SESSION_KEY']


Which makes sense. But what are some preferred work arounds for this issue?

解决方案

In my comment, I suggested using rack cookie based sessions, but looking into it, the Sinatra sessions are Rack cookie sessions anyway.

Looking further, I found this in the Sinatra docs:

To improve security, the session data in the cookie is signed with a session secret. A random secret is generate for you by Sinatra. However, since this secret will change with every start of your application, you might want to set the secret yourself, so all your application instances share it:

set :session_secret, 'super secret'

So it seems each Heroku dyno is generating a different key, and so can't read each others session cookies, and you need to specify a key so each dyno uses the same one.

Rather than add a secret key to your source code, you're probably better setting an environment variable:

$ heroku config:add SESSION_KEY=a_longish_secret_key

Then in your sinatra app:

enable :sessions
set :session_secret, ENV['SESSION_KEY']

这篇关于在Heroku的Sinatra应用程序中,会话不是跨Dynos共享的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆