我需要在一些非常基础的事情上得到一些帮助。我遵循了有关如何让Rails3和Authlogic协同工作的优秀教程。它是在许多地方引用的相同的非常基本的身份验证shell。但是我就是不能让注销功能工作。我花了几天时间尝试各种各样的东西。所以我需要帮助。也许这只是我没有正确理解的事情,但这里有个问题:
底线:@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一个新的会话被创建,然后我可以做之前的工作,得到我的心的内容。所有的操作都是正确的,比如使用其他用户的cookie不能工作,不使用cookie不能工作等等。
但是..。当我注销的时候...执行此操作->
curl -v -H "Accept: text/xml" -X DELETE --cookie cookies.txt http://localhost:3000/user_session.xmlUserSessionsController#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@user_session = current_user_session中的UserSession.find -生成已注销的用户,并将继续这样做...如果提供了登录时创建的原始cookie,则永远。
无论我怎么尝试,我都不能退出会话。当我看到缓存命中发生时,我甚至深入研究了关闭ActiveRecord缓存。缓存已关闭,并且未发生任何更改。我认为这要么是我理解的一个严重错误,要么是Authlogic的一些我不理解或不能工作的东西。
有什么想法吗?
发布于 2011-01-06 00:44:59
基于Zabba的出色回答,我提出了在注销时清理会话记录以及在创建后的预定时间后清理会话记录的解决方案(分别-我希望两者都有)。Zabba的答案很好,我只是想在Authlogic中集成一个答案。
解决方案非常简单和容易。在我找到以下两个资源之前,我并不清楚该怎么做。我想我之前没有找到这些,因为我不知道我在找什么。
用于在注销时清除会话-我使用了这个答案,它工作得很好。
Authlogic and multiple sessions for the same user
对于超时,我在这里使用了第二个答案-
Ruby on rails - Authlogic : periodically check if user session is valid
一切似乎都运行得很好。
发布于 2010-12-26 00:27:49
如果你还没有找到解决这个问题的方法--会话记录永远不会被清理。解决这个问题的一种方法是使用cron作业定期清理会话。
这里有一个我觉得很有用的资源:Sessions and cookies in Ruby on Rails
您还可以安装一个插件,用于更智能地管理用户会话(包括自动清理功能):Limited Sessions
https://stackoverflow.com/questions/4474775
复制相似问题