如何使用 Rails 2.3.8 在子域之间共享会话

发布于 2024-09-19 15:31:04 字数 349 浏览 3 评论 0原文

我发现许多帖子描述了如何执行此操作。它们看起来都像是将其放入适当的环境配置文件中:

config.action_controller.session[:domain] = '.localhost'

但是,如果我这样做,则尝试登录(我正在使用设计)会失败:

ActionController::InvalidAuthenticityToken

我看到其他人发布了相同的问题(到各个博客的评论部分)提供设置 session[:domain] 的建议),但我还没有找到任何人回答了为什么会发生这种情况以及如何修复它的问题。

有什么想法吗?

I have found numerous posts that describe how to do this. They all look something like putting this in the appropriate environment config file:

config.action_controller.session[:domain] = '.localhost'

However, if I do this then trying to sign in (I am using devise) fails with:

ActionController::InvalidAuthenticityToken

I see others posting the same problem (to the comments section of the various blogs offering the advice to set session[:domain]) but I haven't found a case where anybody has answered the question about why that is happening and how to fix it.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-09-26 15:31:05

我不确定这是否与您的问题有关,但是您是否尝试将会话域设置为“.localhost”?这不会起作用,因为它实际上是您尝试为其设置 cookie 的顶级域。

请参阅http://www.ruby-forum.com/topic/181650#794923

I'm not sure if this is related to your problem, but are you trying to set the session domain to just '.localhost'? This won't work as it effectively a top-level domain that you are trying to set a cookie for.

See http://www.ruby-forum.com/topic/181650#794923

黑凤梨 2024-09-26 15:31:05

我在 config/initializers/set_session_domain.rb 中有这个片段:

module ActionControllerExtensions
  def self.included(base)
    base::Dispatcher.send :include, DispatcherExtensions
  end

  module DispatcherExtensions
    def self.included(base)
      base.send :before_dispatch, :set_session_domain
    end

    def set_session_domain
      domain = @env['HTTP_HOST'].gsub(/:\d+$/, '').gsub(/^[^.]*/, '')
      @env['rack.session.options'].update :domain => domain
    end
  end
end

ActionController.send :include, ActionControllerExtensions

一切都工作得很好。

I have this snippet in config/initializers/set_session_domain.rb:

module ActionControllerExtensions
  def self.included(base)
    base::Dispatcher.send :include, DispatcherExtensions
  end

  module DispatcherExtensions
    def self.included(base)
      base.send :before_dispatch, :set_session_domain
    end

    def set_session_domain
      domain = @env['HTTP_HOST'].gsub(/:\d+$/, '').gsub(/^[^.]*/, '')
      @env['rack.session.options'].update :domain => domain
    end
  end
end

ActionController.send :include, ActionControllerExtensions

Everything works pretty nice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文