【问题标题】:Rails/Authlogic - UserSession.destroy does not terminate the sessionRails/Authlogic - UserSession.destroy 不会终止会话
【发布时间】:2010-12-17 21:10:17
【问题描述】:

我需要一些非常基本的帮助。我遵循了关于让 Rails 3 和 Authlogic 一起工作的优秀教程。它与许多地方引用的相同非常基本的身份验证外壳程序相同。但我就是不能让注销功能起作用。我花了几天时间尝试各种事情。所以我需要帮助。也许这只是我没有正确理解的东西,但问题是:

底线:@user_session.destroy 不会终止会话。

详情: 我正在开发一个 Web 服务,所以我只与 .xml 格式的请求进行交互......并且我正在使用 cURL 进行测试。

除了我的主要问题外,一切正常。我用用户播种数据库。

当我在没有先登录的情况下执行以下操作并且 cookies.txt 为空白(或者是其他用户的 cookie)时 - >

curl -v -H "Accept: text/xml" -X GET --cookie cookies.txt http://localhost:3000/user.xml 

我的请求按预期被拒绝(没有会话)

当我使用这个登录时 ->

curl -v -H "Accept: text/xml" -X POST -d 'user_session[email]=eric@xyz.com&user_session[password]=secret' --cookie-jar cookies.txt http://localhost:3000/user_session.xml

创建了一个新会话,然后我可以执行之前的 GET 操作。一切正常,因为使用其他用户的 cookie 不起作用,不使用 cookie 不起作用等等。

但是...当我注销时...这样做 ->

curl -v -H "Accept: text/xml" -X DELETE --cookie cookies.txt http://localhost:3000/user_session.xml

调用 UserSessionsController#destroy 操作

def destroy
  @user_session = UserSession.find
  @user_session.destroy

respond_to do |format|
  format.html { redirect_to(:users, :notice => 'Goodbye!') }
  format.xml  { head :ok }
end
end

以及操作中的行 - @user_session = UserSession.find 和 @user_session.destroy 被执行。没有错误并在销毁后立即测试@user_session = UserSession.find 按预期产生 nil

但是...在下一个 GET 请求中,如果在请求中传递了现在应该过期的同一个 cookie,则会发生对该特定用户 ID 的相同数据库查找并传递信息 - 就好像会话继续存在一样! !

我知道这是基本的东西,但为了完整起见,这里是我的 ApplicationController:

class ApplicationController < ActionController::Base
#protect_from_forgery
helper_method :current_user_session, :current_user

private

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
 return @current_user if defined?(@current_user)
 @current_user = current_user_session && current_user_session.record
end

def require_user
 unless current_user
  respond_to do |format|
    format.xml  { head :unauthorized }
  end
  return false
 end
end

def require_no_user
 if current_user
  respond_to do |format|
   format.xml  { head :bad_request }
  end
 return false
end
end

无论我如何尝试,我都无法退出会话。当我看到缓存命中发生时,我什至钻研关闭 ActiveRecord 缓存。缓存已关闭,没有任何改变。我认为这要么是我理解的严重错误,要么是我不理解或无法正常工作的 Authlogic。

有什么想法吗??

【问题讨论】:

    标签: ruby-on-rails authlogic


    【解决方案1】:

    基于 Zabba 的出色回答,我想出了在创建后的预定时间后清理注销时和(分别 - 我想要两者)会话记录的解决方案。 Zabba 的回答很好,我只是想在 Authlogic 中集成一个答案。

    解决方案非常简单易行。在我找到以下两个资源之前,我并不清楚该怎么做。我想我之前没有找到这些,因为我不知道我在寻找什么。

    为了在注销时清除会话 - 我使用了这个答案并且效果很好。

    Authlogic and multiple sessions for the same user

    对于超时,我在这里使用了第二个答案 -

    Ruby on rails - Authlogic : periodically check if user session is valid

    一切似乎都运行良好。

    【讨论】:

      【解决方案2】:

      如果您还没有找到解决此问题的方法 - 会话记录实际上永远不会被清理。解决此问题的一种方法是使用 cron 作业定期清理会话。

      这是我发现有用的资源:@​​987654321@

      您还可以安装一个插件来更智能地管理用户会话(包括自动清理功能):Limited Sessions

      【讨论】:

      • 谢谢!不,我没有找到一个好的解释或解决方法。我真的很感激帮助。很抱歉延迟回复,但假期我很远。我很惊讶我在搜索中没有遇到 Rails 的这个“功能”。
      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多