gpt4 book ai didi

ruby-on-rails - Rails/Authlogic - UserSession.destroy 不会终止 session

转载 作者:行者123 更新时间:2023-12-01 01:31:33 27 4
gpt4 key购买 nike

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

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

细节:
我正在开发一个网络服务,所以我只与 .xml 格式的请求进行交互......我正在使用 cURL 进行测试。

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

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

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

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

当我使用此登录时 ->
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

创建了一个新 session ,然后我可以对我的内心内容执行先前的 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 进行相同的数据库查找并传递信息 - 就好像 session 继续存在一样!

我知道这是基本的东西,但为了完整起见,这里是我的 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 = UserSession.find in current_user_session - 生成已注销的用户并将继续这样做......如果提供登录时创建的原始 cookie。

无论我尝试什么,我都无法退出 session 。当我看到缓存命中发生时,我什至深入研究关闭 ActiveRecord 缓存。缓存关闭,没有任何变化。我认为这要么是我理解的严重错误,要么是我不理解或不起作用的 Authlogic。

有什么想法吗??

最佳答案

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

解决方案非常简单和容易。在我找到以下两个资源之前,我不清楚如何去做。我想我之前没有找到这些,因为我不知道我在找什么。

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

Authlogic and multiple sessions for the same user

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

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

一切似乎都运行良好。

关于ruby-on-rails - Rails/Authlogic - UserSession.destroy 不会终止 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4474775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com