在 Rails 中的子域之间共享会话(cookie)? [英] Share session (cookies) between subdomains in Rails?

查看:29
本文介绍了在 Rails 中的子域之间共享会话(cookie)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设置,其中每个用户都属于一个公司,并且该公司有一个子域(我使用的是 basecamp 样式的子域).我面临的问题是 rails 正在创建多个 cookie(一个用于 lvh.me,另一个用于 subdomain.lvh.me),这导致我的应用程序出现了很多中断(例如,尽管所有请求一次,闪存消息仍然存在)已登录).

I have an app setup where each user belongs to a company, and that company has a subdomain (I am using basecamp style subdomains). The problem that I am facing is that rails is creating multiple cookies (one for lvh.me and another for subdomain.lvh.me) which is causing quite a few breaks in my application(such as flash messages being persistent though out all requests once signed in).

我的/cofig/initilizers/session_store.rb 文件中有这个:

I have this in my /cofig/initilizers/session_store.rb file:

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

域::all 似乎是我在 Google 上找到的标准答案,但这似乎对我不起作用.任何帮助表示赞赏!

The domain: :all seems to be the standard answer I found on Google, but that doesn't seem to be working for me. Any help is appreciated!

推荐答案

事实证明 'domain: all' 为该会话期间访问的所有不同子域创建了一个 cookie(并确保它们在要求).如果没有传递域参数,则意味着为在同一会话中访问的每个不同域创建一个新 cookie,旧的将被丢弃.我需要的是一个在整个会话中持续存在的 cookie,即使域发生变化.因此,传递 domain: "lvh.me" 解决了开发中的问题.这将创建一个 cookie,该 cookie 保留在不同的子域之间.

As it turns outs 'domain: all' creates a cookie for all the different subdomains that are visited during that session (and it ensures that they are passed around between request). If no domain argument is passed, it means that a new cookie is created for every different domain that is visited in the same session and the old one gets discarded. What I needed was a single cookie that is persistent throughout the session, even when the domain changes. Hence, passing domain: "lvh.me" solved the problem in development. This creates a single cookie that stays there between different subdomains.

对于需要进一步解释的人,这是一个很好的链接:http://excid3.com/博客/sharing-a-devise-user-session-across-subdomains-with-rails-3/

For anyone needing further explanation, this is a great link: http://excid3.com/blog/sharing-a-devise-user-session-across-subdomains-with-rails-3/

这篇关于在 Rails 中的子域之间共享会话(cookie)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆