在两个 Rails 个不同版本的应用程序之间共享 Devise 会话 cookie
Share Devise session cookie between two Rails apps of different versions
我正在尝试在同一 TLD 但位于不同子域上的两个 Rails 应用程序之间共享 Devise 会话 cookie 以进行身份验证。一个应用程序是 v4.2.11.1,另一个是 v6.0.3.2。我想登录 Rails 4 应用程序,并在 Rails 6 应用程序中访问经过身份验证的用户信息。登录 Rails 4 应用程序时,会话 cookie 设置正常,但在 Rails 6 应用程序中尝试访问它时,它似乎被完全擦除 out/reset。
- 两个应用程序的会话存储 cookie 域设置正确,例如
.example.com
.
- 两个应用程序中的会话存储
tld_length
都设置为 2
。
- Cookie 序列化程序在两个应用程序中都设置为
:marshal
。
- 我在两个应用程序中使用相同的
secret_key_base
。在 Rails 4 应用程序中,它是通过 ENV['SECRET_KEY_BASE']
环境变量设置的。在 Rails 6 应用程序中,它是通过 Rails 凭据设置的,例如config/credentials/<env>.yml.enc
.
Devise.secret_key
在两个应用程序中相同。
- 我在两个应用程序中使用相同的 Devise gem 版本和初始值设定项。
在您的特定情况下可能还有其他事情发生,但是自 Rails 4 以来会话 cookie 有两次 backward-incompatible 更改是毫无价值的,您需要查看这些更改在
- Rails5.2 中发生了变化,将过期信息嵌入到加密的 cookie 中。来自 upgrade guide:
To improve security, Rails now embeds the expiry information also in
encrypted or signed cookies value.
This new embed information make those cookies incompatible with
versions of Rails older than 5.2.
If you require your cookies to be read by 5.1 and older, or you are
still validating your 5.2 deploy and want to allow you to rollback set
Rails.application.config.action_dispatch.use_authenticated_cookie_encryption
to false
.
- Rails 6.0 更改为在加密 cookie 中嵌入用途。从
升级
指南:
To improve security, Rails embeds the purpose information in encrypted
or signed cookies value. Rails can then thwart attacks that attempt to
copy the signed/encrypted value of a cookie and use it as the value of
another cookie.
This new embed information make those cookies incompatible with
versions of Rails older than 6.0.
If you require your cookies to be read by Rails 5.2 and older, or you
are still validating your 6.0 deploy and want to be able to rollback
set Rails.application.config.action_dispatch.use_cookies_with_metadata
to false
.
我正在尝试在同一 TLD 但位于不同子域上的两个 Rails 应用程序之间共享 Devise 会话 cookie 以进行身份验证。一个应用程序是 v4.2.11.1,另一个是 v6.0.3.2。我想登录 Rails 4 应用程序,并在 Rails 6 应用程序中访问经过身份验证的用户信息。登录 Rails 4 应用程序时,会话 cookie 设置正常,但在 Rails 6 应用程序中尝试访问它时,它似乎被完全擦除 out/reset。
- 两个应用程序的会话存储 cookie 域设置正确,例如
.example.com
. - 两个应用程序中的会话存储
tld_length
都设置为2
。 - Cookie 序列化程序在两个应用程序中都设置为
:marshal
。 - 我在两个应用程序中使用相同的
secret_key_base
。在 Rails 4 应用程序中,它是通过ENV['SECRET_KEY_BASE']
环境变量设置的。在 Rails 6 应用程序中,它是通过 Rails 凭据设置的,例如config/credentials/<env>.yml.enc
. Devise.secret_key
在两个应用程序中相同。- 我在两个应用程序中使用相同的 Devise gem 版本和初始值设定项。
在您的特定情况下可能还有其他事情发生,但是自 Rails 4 以来会话 cookie 有两次 backward-incompatible 更改是毫无价值的,您需要查看这些更改在
- Rails5.2 中发生了变化,将过期信息嵌入到加密的 cookie 中。来自 upgrade guide:
To improve security, Rails now embeds the expiry information also in encrypted or signed cookies value.
This new embed information make those cookies incompatible with versions of Rails older than 5.2.
If you require your cookies to be read by 5.1 and older, or you are still validating your 5.2 deploy and want to allow you to rollback set
Rails.application.config.action_dispatch.use_authenticated_cookie_encryption
tofalse
.
- Rails 6.0 更改为在加密 cookie 中嵌入用途。从 升级 指南:
To improve security, Rails embeds the purpose information in encrypted or signed cookies value. Rails can then thwart attacks that attempt to copy the signed/encrypted value of a cookie and use it as the value of another cookie.
This new embed information make those cookies incompatible with versions of Rails older than 6.0.
If you require your cookies to be read by Rails 5.2 and older, or you are still validating your 6.0 deploy and want to be able to rollback set
Rails.application.config.action_dispatch.use_cookies_with_metadata
tofalse
.