Rails 3个额外的会话配置选项(key,expires_after,secure)

she*_*edd 24 ruby ruby-on-rails session-variables session-state ruby-on-rails-3

有人可以指出新的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.
}
Run Code Online (Sandbox Code Playgroud)

但是,在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'
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢!

fid*_*the 40

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

Your::Application.config.session_store :cookie_store, :key => '_session'
Run Code Online (Sandbox Code Playgroud)

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

Your::Application.config.session_store :cookie_store, {
  :key =>           '_session_id',
  :path =>          '/',
  :domain =>        nil,
  :expire_after =>  nil,
  :secure =>        false,
  :httponly =>      true,
  :cookie_only =>   true
}
Run Code Online (Sandbox Code Playgroud)

(这些只是标准默认值)

如果你在生产中强制使用SSL,那么在cookie上设置secure不应该在实践中真正有所作为,但是你可能想要设置它只是为了安全起见......

Your::Application.config.session_store :cookie_store, {
  :key =>           '_session_id',
  :secure =>        Rails.env.production?
}
Run Code Online (Sandbox Code Playgroud)

  • 而且,只是为了澄清,似乎这些选项也适用于active_record会话存储.我注意到上面的代码示例是针对cookie存储区编码的,但我尝试使用active_record存储(问题就是这个问题)并且选项似乎生效了. (2认同)