简体   繁体   English

使用设计和会话存储的 Rails 动态会话超时

[英]Rails dynamic session timeout using devise and session store

I know i can do dynamic session timeout (per user) with devise like described here我知道我可以使用这里描述的设备进行动态会话超时(每个用户)

This worked fine before but now i'm using rails session store and it doesn't work anymore.这以前工作得很好,但现在我正在使用 rails session store 并且它不再工作了。

I googled quite a long time but didn't find an answer, does anyone know how to do this?我用谷歌搜索了很长时间,但没有找到答案,有人知道怎么做吗?

Thanks谢谢

I've done it now with some before_filters:我现在已经用一些 before_filters 完成了:

class ApplicationController < ActionController::Base
  before_filter :update_session, :check_if_session_is_valid

  ...
  def check_if_session_is_valid
    session_timeout = current_user.try(:session_timeout) || 1800
    if session[:timestamp] <= session_timeout.seconds.ago.to_i
      session[:user_id] = nil
    end
  end

  def update_session
    session[:timestamp] = Time.now.to_i
  end

And in the controller which is used by the poller i added this (the session shouldn't get updated by the poller):在轮询器使用的控制器中,我添加了这个(轮询器不应更新会话):

skip_before_filter :update_session, :only => :get_new

The most simplest approach for session timeout using devise would be to use Devise 'timeoutable' module.使用 devise 会话超时的最简单方法是使用 Devise 'timeoutable' 模块。 Following 2 lines will serve the session timeout functionality, Only if u are using devise in your Rails application.以下 2 行将提供会话超时功能,仅当您在 Rails 应用程序中使用设计时。

Step 1: In your User.rb Model declare this第 1 步:在你的 User.rb 模型中声明这个

 devise: timeoutable

Step 2:第2步:

in app/config/initializers/devise.rb add following line, default set by Devise is 30 minutes.app/config/initializers/devise.rb添加以下行,Devise 设置的默认值为 30 分钟。

config.timeout_in = 15.minutes

Hope this works for you!希望这对你有用!

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM