【问题标题】:Share session between some subdomains in Rails?在 Rails 中的某些子域之间共享会话?
【发布时间】:2019-08-18 22:34:33
【问题描述】:

是否可以共享某些特殊子域的会话?例如我有一些子域:

  • admin.example.com
  • manager.example.com
  • user.example.com

和带有正则表达式的动态子域:user.*.example.com (user.sub1.example.com)

当用户通过user.example.com 登录时=> 自动登录user.*.example.com 但未登录admin.example.com

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails wildcard-subdomain


    【解决方案1】:

    将此添加到您的 /config/initilizers/session_store.rb 文件中:

    AppName::Application.config.session_store :cookie_store, key: '_application_devise_session', domain: :all
    

    'domain: all' 为该会话期间访问的所有不同子域创建一个 cookie(并确保它们在请求之间传递)。如果没有传递域参数,则意味着为在同一会话中访问的每个不同域创建一个新的 cookie,而旧的将被丢弃。

    最终是该表达式中的 tld_length(顶级域长度)。例如,默认 tld_length 为 1,而 manager.example.come 的 tld_length 为 2,而 127.0.0.1.example.com 的 tld_length 为 5。所以我在 session_store.rb 文件中的 example.com 上的子域在开发中以及在生产中的任何其他内容如下。

    AppName::Application.config.session_store :cookie_store, key: '_application_devise_session', domain: :all, tld_length: 2
    

    要配置每个环境,您可以使用以下内容:

    Rails.application.config.session_store :cookie_store, key: '_my_app_session', domain: {
      production: '.example.com',
      development: '.example.dev'
    }.fetch(Rails.env.to_sym, :all)
    

    来源:https://github.com/plataformatec/devise/wiki/How-To:-Use-subdomains

    【讨论】:

    • 它将为所有子域(.example.com)发送会话密钥,但出于安全原因,我只想为特殊域发送会话。 (user.example.comuser.*.example)有可能吗?
    【解决方案2】:

    https://www.botreetechnologies.com/blog/how-to-share-session-between-rails-4-applications查看解决方案

    该博客讨论了 Rails 4,但该解决方案不仅适用于任何版本的 Rails,而且适用于任何Rack application

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 2016-12-27
      • 2021-12-26
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多