Rails 3 session_store 域 :all 的作用是什么?

2024-04-28

更新了问题以使其更清楚

据我所知,您可以设置 session_store 的域以在子域之间共享会话,如下所示:Rails.application.config.session_store :cookie_store, :key => '_my_key', :domain => "mydomain.example"

在 Rails 3 中,设置是什么:domain => :all做?它不能让您跨顶级域共享会话,cookie 无法做到这一点。该文档称它假定一个顶级域。那么如果多个域访问您的应用程序会发生什么?

在我的应用程序中,我的用户可以创建一个主域的个人子域,但也可以通过自己的自定义域访问该子域。

正确的 session_store 域设置是什么,以便我可以: a)在我的主域的所有域之间共享会话,例如mydomain.exampleb) 通过 CNAME 自定义 URL 访问其个人子域(例如“user1.mydomain.example”)的用户,例如some.otherdomain.example仍然可以创建单独的会话。


好的,实现此目的的方法是动态设置会话 cookie 上的域。为了尽早做到这一点,应该将其作为机架中间件来完成:

# Custom Domain Cookie
#
# Set the cookie domain to the custom domain if it's present
class CustomDomainCookie
  def initialize(app, default_domain)
    @app = app
    @default_domain = default_domain
  end

  def call(env)
    host = env["HTTP_HOST"].split(':').first
    env["rack.session.options"][:domain] = custom_domain?(host) ? ".#{host}" : "#{@default_domain}"
    @app.call(env)
  end

  def custom_domain?(host)
    host !~ /#{@default_domain.sub(/^\./, '')}/i
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 3 session_store 域 :all 的作用是什么? 的相关文章

随机推荐