简体   繁体   English

如何使用Rails在多个域之间共享用户会话?

[英]How can I share user sessions across multiple domains using Rails?

Is anyone aware of any gems, tutorials, or solutions enabling a user to sign in to a website at one domain and automatically given access to other partner domains in the same session? 是否有人知道任何宝石,教程或解决方案使用户能够在一个域中登录网站并自动在同一会话中访问其他合作伙伴域?

I have two rails apps running, let's call them App-A and App-B. 我有两个rails应用程序运行,我们称之为App-A和App-B。 App-A has a database associated with it, powering the registration and login at App-A.com. App-A有一个与之关联的数据库,为App-A.com的注册和登录提供支持。 I'd now like to give all of those users with App-A.com accounts access to App-B.com, without making them reregister or manually login to App-B.com separately. 我现在想让App-A.com帐户的所有用户访问App-B.com,而无需重新注册或手动登录App-B.com。

Thanks in advance for any help! 在此先感谢您的帮助! --Mark - 标记

You can set the same session_key in both apps. 您可以在两个应用中设置相同的session_key。 In appA environment.rb change the session_key, like this 在appA environment.rb中更改session_key,就像这样

Rails::Initializer.run do |config|
   ...  
 config.action_controller.session = {
   :session_key => '_portal_session',
   :secret      => '72bf006c18d459acf51836d2aea01e0afd0388f860fe4b07a9a57dedd25c631749ba9b65083a85af38bd539cc810e81f559e76d6426c5e77b6064f42e14f7415'
  }
  ...
end

Do the same in AppB. 在AppB中做同样的事情。 (remember to use the very same secret) (记得用同样的秘密)

Now you have shared sessions. 现在你有共享会话。 Let's say you use restfull_authentication, wich sets a session variable called user_id . 假设你使用restfull_authentication,它设置一个名为user_id的会话变量。 When you authenticate in appA it sets the user_id in the session. 在appA中进行身份验证时,会在会话中设置user_id。 Now, in appB you just have to verify if user_id exists in the session. 现在,在appB中,您只需验证会话中是否存在user_id。

This is the overall schema, you can elaborate more using this idea. 这是整体架构,您可以使用这个想法详细说明。

If you want to create single sign-on solution for your applications then I recommend to take a look at RubyCAS solution. 如果您想为您的应用程序创建单点登录解决方案,那么我建议您查看RubyCAS解决方案。 It could be used also to provide single sign-on for other non-Rails applications as well as you can integrate authentication with LDAP or other authentication providers. 它还可以用于为其他非Rails应用程序提供单点登录,以及您可以将身份验证与LDAP或其他身份验证提供程序集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM