【问题标题】:Ruby on rails - Authlogic : periodically check if user session is validRuby on rails - Authlogic:定期检查用户会话是否有效
【发布时间】:2011-01-13 07:27:51
【问题描述】:

我正在寻找一种解决方案,允许我定期检查用户会话是否已过期,如果已过期,请将他重定向到登录页面。
我正在使用 Authlogic gem,所以我正在做的是调用一个对 current_user 进行测试的函数。
我的 USER_SESSION_TIMEOUT 是 5 分钟,所以我每 5:10 分钟进行一次 ajax 调用。

<%= periodically_call_remote :url => {:controller => 'user_session', :action => 'check_session_timed_out'}, :frequency =>  (USER_SESSION_TIMEOUT + 10.seconds) %>

def check_session_timed_out
   if !current_user
      flash[:login_notice] = "Your session timed out due to a period of inactivity. Please sign in again."
      render :update do |page|
          page.redirect_to "/user_sessions/new"   
      end
   else
       render :nothing => true
   end
end

我注意到每次调用 current_user 时,用户对象都会更新,因此会话会更新为 5 分钟。
只打开一个选项卡时没有问题,但如果我每次调用 check_session_timed_out current_user renew 时都有 2 个选项卡,则更新用户,因此会话永不过期。

有什么想法吗? 谢谢

【问题讨论】:

    标签: ruby-on-rails session authlogic session-timeout


    【解决方案1】:

    Authlogic 可以为您做到这一点。只需在您的模型中使用:

    在用户模型上:

    acts_as_authentic do |c|
      c.logged_in_timeout(5.minutes)
    end
    

    ...在 UserSession 模型上:

    self.logout_on_timeout = true
    

    简单地工作! =D

    【讨论】:

      【解决方案2】:

      来自 AuthLogic source itself

      # For example, what if you had a javascript function that polled the server
      # updating how much time is left in their session before it times out. Obviously
      # you would want to ignore this request, because then the user would never
      # time out. So you can do something like this in your controller:
      
      def last_request_update_allowed?
       action_name != "update_session_time_left"
      end
      

      在您的情况下,您可能希望使用您的操作名称将该方法添加到您的控制器:

      def last_request_update_allowed?
        action_name != "check_session_timed_out"
      end
      

      【讨论】:

        猜你喜欢
        • 2013-09-26
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多