在Rails 3中设置会话超时 [英] Setting session timeout in Rails 3

查看:382
本文介绍了在Rails 3中设置会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单:我试图让我的导轨活动记录会话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.

我只是跑轨道在我的本地开发机服务器(即WebBrick)。

I'm just running rails server (i.e. WebBrick) on my local dev machine.

我知道这是什么做以下code。在配置/ 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.

可以有一个人帮助我吗?

Can some one help me out?

推荐答案

我想你将不得不这样做手工,因为活动记录存储不执行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版本中轨present。我找到了相关的code <一个href="https://github.com/rails/rails/blob/2-3-stable/actionpack/lib/action_controller/session/abstract_store.rb#L175">here.这是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屋!

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