包括在 cookie jar 中以允许链接,例如 cookies.permanent.signed
。
实例公共方法
encrypted() 链接
返回一个 jar,该 jar 会在将 cookie 值发送给客户端之前自动对其进行加密,并在读取时对其进行解密。如果用户(或第三方)篡改了 cookie,则会返回 nil
。
如果 config.action_dispatch.encrypted_cookie_salt
和 config.action_dispatch.encrypted_signed_cookie_salt
都已设置,则使用 HMAC AES-256-CBC 加密的旧版 cookie 会被透明地升级。
此 jar 要求您为应用程序的 secret_key_base
设置一个合适的验证密钥。
示例
cookies.encrypted[:discount] = 45
# => Set-Cookie: discount=DIQ7fw==--K3n//8vvnSbGq9dA--7Xh91HfLpwzbj1czhBiwOg==; path=/
cookies.encrypted[:discount] # => 45
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/middleware/cookies.rb, line 258 def encrypted @encrypted ||= EncryptedKeyRotatingCookieJar.new(self) end
permanent() 链接
返回一个 jar,该 jar 会自动将分配的 cookie 设置为在 20 年后过期。示例
cookies.permanent[:prefers_open_id] = true
# => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
此 jar 仅用于写入。您将通过常规访问器读取永久 cookie。
此 jar 还允许与 signed jar 链接,以便您可以设置永久的 signed cookie。示例
cookies.permanent.signed[:remember_me] = current_user.id
# => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/middleware/cookies.rb, line 224 def permanent @permanent ||= PermanentCookieJar.new(self) end
signed() 链接
返回一个 jar,它将自动生成 cookie 值的签名表示形式,并在再次从 cookie 中读取时对其进行验证。这对于创建用户不应该更改其值 cookie 很有用。如果用户(或第三方)篡改了签名 cookie,则将返回 nil
。
此 jar 要求您为应用程序的 secret_key_base
设置一个合适的验证密钥。
示例
cookies.signed[:discount] = 45
# => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
cookies.signed[:discount] # => 45
源代码:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/middleware/cookies.rb, line 240 def signed @signed ||= SignedKeyRotatingCookieJar.new(self) end
signed_or_encrypted() 链接
返回 signed
或 encrypted
jar,如果设置了 secret_key_base
,则优先选择 encrypted
。由 ActionDispatch::Session::CookieStore
使用,以避免需要引入新的 cookie 存储。
源代码:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/middleware/cookies.rb, line 264 def signed_or_encrypted @signed_or_encrypted ||= if request.secret_key_base.present? encrypted else signed end end