【问题标题】:Rails 3 additional session configuration options (key, expires_after, secure)Rails 3 个额外的会话配置选项(key、expires_after、secure)
【发布时间】:2011-09-17 20:40:30
【问题描述】:

谁能指出新的 Rails 3.x 会话配置选项是什么?

我正在尝试复制我在 Rails 2.3.x 应用程序中的相同配置。

这是我在应用程序中使用的配置:

#environment.rb
config.action_controller.session_store = :active_record_store

config.action_controller.session = {
    :key         => '_something', #non-secure for development
    :secret      => 'really long random string'
  }


# production.rb - override environment.rb for production
config.action_controller.session = {
  :key            => '_something_secure',
  :secret         => 'really long random string',
  :expire_after   => 60*60,#time in seconds
  :secure         => true #The session will now not be sent or received on HTTP requests.
}

但是,在 Rails 3.x 中,我只能找到以下内容:

AppName::Application.config.session_store :active_record_store

AppName::Application.config.secret_token = 'really long random string'

AppName::Application.config.cookie_secret = 'another really long random string'

是否有其他配置设置来控制密钥、expire_after 时间和安全选项?

关于后者,如果 production.rb 中设置了“config.force_ssl = true”,我认为不再需要安全选项?

非常感谢!

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 session-state session-variables


【解决方案1】:

您现在通过初始化程序配置基于 Cookie 的会话存储,可能在 config/initializers/session_store.rb 中。在 Rails 3 中,会话存储是一个中间件,配置选项通过一次调用 config.session_store 传入:

Your::Application.config.session_store :cookie_store, :key => '_session'

您可以使用:key 在哈希中添加任何您想要的额外选项,例如

Your::Application.config.session_store :cookie_store, {
  :key =>           '_session_id',
  :path =>          '/',
  :domain =>        nil,
  :expire_after =>  nil,
  :secure =>        false,
  :httponly =>      true,
  :cookie_only =>   true
}

(这些只是标准默认值)

如果您在生产中强制使用 SSL,那么在 cookie 上设置安全在实践中应该不会真正产生影响,但您可能希望设置它只是为了安全起见...

Your::Application.config.session_store :cookie_store, {
  :key =>           '_session_id',
  :secure =>        Rails.env.production?
}

【讨论】:

  • 看起来就是这样!谢谢!
  • 另外,澄清一下,这些选项似乎也适用于 active_record 会话存储。我注意到上面的代码示例是为 cookie 存储编码的,但是我用 active_record 存储尝试了这些(这是问题所问的)并且选项似乎生效了。
  • 调试旧项目,感谢您节省我的时间。
  • 如果 session_store 是 active_record 是否需要 :key 参数?目前我正在从 3.0 迁移到 3.2,rails guide 说要更改那个键 edgeguides.rubyonrails.org/… 。 key 参数的值是否与 secret_token 相关?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 2011-08-17
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多