简体   繁体   English

如何允许Rails 3应用读取Rails 5 cookie?

[英]How to allow Rails 3 app to read Rails 5 cookies?

I have an old application written on top of Rails 3. The app has a home-rolled authentication system and uses cookies extensively for session, user id, role, etc., information. 我在Rails 3的顶部编写了一个旧应用程序。该应用程序具有家用认证系统,并广泛使用cookie来获取会话,用户ID,角色等信息。 Although the app is built on Rails 3, it is not a Rails application and the decision has been made to slowly replace it in parts. 尽管该应用程序是基于Rails 3构建的,但它不是Rails应用程序,因此已决定逐步替换它。

We will be putting nginx in front of the old and new apps and using nginx as a pass-through proxy to route different url paths to one of the two applications. 我们将把nginx放在新旧应用程序的前面,并使用nginx作为传递代理将不同的URL路径路由到两个应用程序之一。

Catch is that the code can't be changed on the Rails 3 app because it will invalidate all existing cookies. 值得注意的是,该代码无法在Rails 3应用上更改,因为它将使所有现有cookie无效。

The new, Rails 5, app will be used for authentication and the idea is for it to create a cookie that the old, Rails 3, application can read and use as a single sign on solution. 新的Rails 5应用程序将用于身份验证,其想法是创建一个cookie,旧的Rails 3应用程序可以读取该cookie并将其用作单一登录解决方案。

For the Rails 5 app, we will be using Devise. 对于Rails 5应用程序,我们将使用Devise。 Rails 5 has encrypted cookies and Rails 3 uses signed encoded cookies. Rails 5具有加密的cookie,而Rails 3使用签名的已编码cookie。 Is there any way of telling Rails 5 to create older Rails 3 style cookies? 有什么方法可以告诉Rails 5创建旧的Rails 3风格的cookie?

The code in the Rails 3 app cannot be changed because it would invalidate all existing cookies. 无法更改Rails 3应用程序中的代码,因为它会使所有现有的cookie失效。

If you cant change the rails3 app, then you have to tell the Rails5 to use cookies that the rails 3 app can read. 如果您无法更改rails3应用程序,则必须告诉Rails5使用rails 3应用程序可以读取的cookie。 Rails 4.1 and earlier use 'Marshal' to serialize the cookies, but Rails 5 defaults to :json for serializing. Rails 4.1和更早版本使用'Marshal'来序列化cookie,但是Rails 5默认使用:json进行序列化。

If you were just updating a single app, then the solution would be to use :hypbrid serialization, which would slowly migrate all cookies to the new format, however, in your case this would break the Rails3 app. 如果您仅更新单个应用程序,那么解决方案是使用:hypbrid序列化,它将缓慢地将所有cookie迁移到新格式,但是,在您的情况下,这会破坏Rails3应用程序。 So try setting the cookie_serializer in the App/Config/Initializers to :marshal 因此,尝试将App / Config / Initializers中的cookie_serializer设置为:marshal

Rails.application.config.action_dispatch.cookies_serializer = :marshal

You can also look at this question and answer regarding sharing cookies between Rails 3 and 4: Sharing Cookies between rails 3 and 4 您还可以查看有关在Rails 3和4之间共享cookie的问题和答案: 在rails 3和4之间共享cookie。

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

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