Rails - 多个顶级域和单个会话/cookie

发布于 2024-07-10 17:16:40 字数 569 浏览 7 评论 0原文

我已经为此苦苦挣扎了一段时间,但一直无法 找到解决方案。 我需要一个用户能够查看多个顶级 具有单一登录的域。

我的理解是,这需要在 environment.rb 中设置并且 使用 before_dispatch 调用。 这就是我想到的:

require 'activesupport'
require 'dispatcher'
module ActionController
   class Dispatcher

      def set_session_domain
         ActionController::Base.session_options.update :session_domain => "#{@request.host}"
      end 

      before_dispatch :set_session_domain
   end
end

但是,当我尝试提取值时,这似乎不起作用 来自会话[:session_domain]

任何帮助是极大的赞赏。

I've been struggling with this for quite awhile and haven't been able to
find a solution. I need a user to be able to view multiple top level
domains with a single login.

My understanding is that this needs to be set in environment.rb and
called with before_dispatch. This is what I've come up with:

require 'activesupport'
require 'dispatcher'
module ActionController
   class Dispatcher

      def set_session_domain
         ActionController::Base.session_options.update :session_domain => "#{@request.host}"
      end 

      before_dispatch :set_session_domain
   end
end

However, this does not seem to be working when I try and pull the values
from session[:session_domain].

Any help is greatly appreciated.

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

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

发布评论

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

评论(5

无声静候 2024-07-17 17:16:40

这个有点棘手。 由于 cookie 只能分配给当前域(例如“forms.example.com”)和父域(“.example.com”,但不能分配给“.com”)(并从中检索),但不能分配给其他域(“othersite.com”),您必须找到另一个解决方案。 这与 Rails 无关,而是与 cookie 的工作方式有关。

编辑:会话依赖于存储在 cookie 中的特定于客户端的句柄,这就是会话也不能跨域工作的原因。

此站点有一种可能的解决方案用于创建跨域 cookie,并且这是我所知道的最干净的方式,尽管它可能会产生一些安全隐患。 更复杂的版本将让服务器通过某些安全通道直接通信。

如果您正在寻找更通用的单点登录服务,请尝试实现某种形式的 OpenID

This one is a bit tricky. Since cookies can only be assigned to (and retrieved from) the current domain ("forms.example.com", say) and parent domains (".example.com", but not ".com"), but NOT to other domains ("othersite.com"), you'll have to find yourself another solution. This has nothing to do with Rails, but with how cookies work.

EDIT: Sessions rely on a client-specific handle, stored in a cookie, which is why sessions also don't work cross-domain.

This site has one possible solution for creating a cross-domain cookie, and it's the cleanest way I know of, although it may have some security implications. A more complicated version would have the servers communicate directly through some secure channel.

If you're looking for a more general-purpose single-login service, try implementing some form of OpenID.

紫竹語嫣☆ 2024-07-17 17:16:40

对于 Rails 2.3 中的子域

ActionController::Base.session = { :domain => ".mydomain.com" }

For sub-domains in Rails 2.3

ActionController::Base.session = { :domain => ".mydomain.com" }
羁客 2024-07-17 17:16:40

如果您希望跨域进行身份验证,无论它们是顶级域还是子域,您可能需要像 RubyCAS 这样的东西。

You will probably need something like RubyCAS if you want authentication across domains regardless of whether they're top-level or subdomains.

不离久伴 2024-07-17 17:16:40

恕我直言,你的问题不够精确。 您是否想要为您拥有的所有 Rails 应用程序使用一个 cookie,还是在单个 cookie 的上下文中? 如果是前者,您想查看使用数据库支持的会话的解决方案或类似 的解决方案RubyCAS 实现 CAS 协议。

Your question is not really precise enough IMHO. Do you want a single cookie for all Rails apps you have or is it within the context of a single one? If the former, you want to look at solutions using database-backed sessions or something along the line of RubyCAS to implement the CAS protocol.

心不设防 2024-07-17 17:16:40

Keltia 和 zuk 都是对的,答案是 ruby​​CAS,我们已经进行了集成,它允许

SSI - 单点登录
您登录到一个站点后会自动登录到另一个

SSO - 单点登录
您从一个站点注销并自动从另一个站点注销

对于我们来说,这是一种经过验证的解决方案,并且实施起来并不困难,

我们正在使用它
http://www.cabslk.com 和 www.ticketslk.com

欢呼,
萨梅拉

Both Keltia and zuk are right, Answer is rubyCAS, We have do that integration and it allows

SSI - Single sign -in
You sign to one site and you are automatically signed to the other

SSO - Single Sign Out
You sign out from one site and automatically you signed out from the other

For us this is a proven solution and not a hard one to implement

we are using it in
http://www.cabslk.com and www.ticketslk.com

cheers,
Sameera

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