在 Rails 3 中设置会话超时 [英] Setting session timeout in Rails 3
问题描述
这看起来很简单:我试图让我的 rails Active Record 会话在 2 分钟后超时.所以两分钟后,我希望我的用户必须重新登录.
This seems simple: I am trying to get my rails Active Record session to timeout after 2 minutes. So after two minutes I want my users to have to re-login.
我只是在我的本地开发机器上运行 rails server
(即 WebBrick).
I'm just running rails server
(i.e. WebBrick) on my local dev machine.
我知道这与 config/initalizers/session_store.rb
中的以下代码有关,但我认为我还没有完全掌握它:
I know this is something to do with the following code in config/initalizers/session_store.rb
, but I don't think I have quite nailed it:
CodedOn::Application.config.session_store :active_record_store
CodedOn::Application.configure do
config.action_controller.session = {:expire_after => 2.minutes}
end
这似乎不起作用,或者至少我的会话似乎没有超时.我找不到太多关于 Rails 3 的方法来做到这一点,因为我知道事情从 Rails 2.x 开始发生了变化.
This doesn't seem to work, or at least my session doesn't appear to timeout. I can't find much about the Rails 3 way to do this as I know things have changed from Rails 2.x.
有人可以帮我吗?
推荐答案
我认为您必须手动执行此操作,因为活动记录存储没有实现 expire_after 选项.所以在你的(我假设)过滤器之前,你应该这样做:
I think you will have to do this manually since the active record store does not implement the expire_after option. So within your (I assume) before filter, you should do this:
def authenticate
if session[:logged_in]
reset_session if session[:last_seen] < 2.minutes.ago
session[:last_seen] = Time.now
else
... authenticate
session[:last_seen] = Time.now
end
end
显然,这并不完整,但它应该为您提供基本概念.
Obviously, this is not complete, but it should give you the basic idea.
更新:
从 2.3 版开始,rails 中似乎存在该功能.我在这里找到了相关代码.这是 AbstractStore,它应该作为所有派生类的基类.因此,正如 dadooda 所建议的,以下应该有效:
It seems that the functionality IS present in rails since version 2.3. I found the relevant code here. This is AbstractStore which should serve as base class for all derived ones. So, as dadooda suggests, the following should work:
Some::Application.config.session_store :active_record_store, {
expire_after: 24.hours,
}
这篇关于在 Rails 3 中设置会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!