在Rails 5中的域之间共享登录cookie /会话 [英] Share a login cookie/session between domains in Rails 5

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

问题描述

要完全清楚,我绝对是指域之间的共享会话,而不仅仅是子域。

To be totally clear, I definitely mean share sessions between domains, not just subdomains.

无论如何,想像一下Tumblr,您可以在其中访问<$来访问博客c $ c>< name> .tumblr.com ,但您也可以将其设置为使用自定义域。

Anyway, imagine Tumblr where you can access a blog by going to <name>.tumblr.com, but you can also set it up to use a custom domain.

重新尝试做类似的事情。您可以照常访问您网站的一部分,也可以对其进行设置,以便可以使用自定义域来访问网站的一部分。

We're trying to do something similar. You can access your part of our site as normal, or you can set it up so that you can use a custom domain to visit your part of the site.

重要这里的部分是,无论使用什么域,都由同一个盒子处理。这本身不是SSO,因为我们是从不同的域访问同一Rails应用。

The important part here is that no matter what domain is used, it's all handled by the same box. This isn't SSO per se, since we are accessing the same Rails app, just from different domains.

无论如何,问题:最好的方法是什么,这样一来,如果用户登录到我们的主站点,则在通过自定义域访问我们的站点时不必再次登录。

Anyway, the question: What is the best way to make it so that if a user is logged in to our main site, they don't have to log in again when visiting our site via a custom domain.

本质上,我们希望这样做,以便用户登录一次,无论他们使用哪个域访问我们的网站,他们都保持登录状态。

Essentially, we want to make it so that a user signs in once and they remain signed in regardless of what domain they used to access our site.

任何建议都值得赞赏!

推荐答案

您可以将会话存储在iframe并通过 postMessage 进行访问。

You can store session in iframe on your main domain and access it by postMessage.

您可以加载包含以下内容的iframe:

You can load iframe contains something like:

parent.postMessage(JSON.stringify({user_id: <%=@user.id %>, token: <%=@token %>, etc...}), '<%= @target_origin || '*'%>');

,此iframe页面上的事件监听器将处理以下消息:

and event listener on page with this iframe process this message:

var listener = function (e) {
  if (e.origin === correctIframeTarget) {
    var data = JSON.parse(e.data);
    // etc...
};

if (window.addEventListener) {
  window.addEventListener('message', listener);
} else {
  window.attachEvent('onmessage', listener);
}

注意:应该在加载iframe之前添加事件监听器。

Notice: event listener should be added before iframe loading.

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

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