跳至内容 跳至搜索
方法
#
C
N
O
P
R

Attributes

[RW] transitional

如果为 true,在构建消息加密器时,将交换前两个轮换选项集。例如,使用以下配置,消息加密器将使用 serializer: Marshal, url_safe: true 加密消息,并能够解密使用这三个选项集之一加密的消息。

encryptors = ActiveSupport::MessageEncryptors.new { ... }
encryptors.rotate(serializer: JSON, url_safe: true)
encryptors.rotate(serializer: Marshal, url_safe: true)
encryptors.rotate(serializer: Marshal, url_safe: false)
encryptors.transitional = true

这在执行应用程序的滚动部署时非常有用,尚未更新的服务器仍然必须能够解密来自已更新服务器的消息。在这种情况下,首先执行一次滚动部署,将新轮换(例如 serializer: JSON, url_safe: true)作为第一个轮换,并将 transitional = true。然后,在所有服务器都更新后,执行第二次滚动部署,并将 transitional = false

类公共方法

new(&secret_generator)

初始化新实例。secret_generator 必须接受一个 salt 和一个 secret_length 关键字参数,并返回一个合适的 secret(字符串)或 secrets(字符串数组)。secret_generator 也可以接受其他任意关键字参数。如果使用 rotate 方法并传入与这些关键字参数匹配的任何选项,这些选项将传递给 secret_generator 而不是传递给消息加密器。

encryptors = ActiveSupport::MessageEncryptors.new do |salt, secret_length:, base:|
  MySecretGenerator.new(base).generate(salt, secret_length)
end

encryptors.rotate(base: "...")
# File activesupport/lib/active_support/message_encryptors.rb, line 34
    

实例公共方法

[](salt)

返回一个使用给定 salt 派生的 secret 配置的 MessageEncryptor,并使用 rotate 的选项。 MessageEncryptor 实例将被记忆化,因此相同的 salt 将返回相同的实例。

# File activesupport/lib/active_support/message_encryptors.rb, line 54
    

[]=(salt, encryptor)

覆盖与给定 salt 关联的 MessageEncryptor 实例。

# File activesupport/lib/active_support/message_encryptors.rb, line 65
    

clear_rotations

清除选项集列表。

# File activesupport/lib/active_support/message_encryptors.rb, line 163
    

on_rotation(&callback)

设置一个回调,当使用非第一个选项集的消息被解密时调用。

例如,此回调可以记录每次调用时的情况,从而指示旧的选项集是否仍在使用中,或者是否可以从轮换中移除。

# File activesupport/lib/active_support/message_encryptors.rb, line 172
    

prepend(**options)
prepend(&block)

rotate 类似,但会将给定的选项或块添加到选项集列表的前面。

当你有一个已经配置好的 MessageEncryptors 实例,但又想覆盖消息的加密方式时,这很有用。

module ThirdParty
  ENCRYPTORS = ActiveSupport::MessageEncryptors.new { ... }.
    rotate(serializer: Marshal, url_safe: true).
    rotate(serializer: Marshal, url_safe: false)
end

ThirdParty.ENCRYPTORS.prepend(serializer: JSON, url_safe: true)

# Uses `serializer: JSON, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: true` or
# `serializer: Marshal, url_safe: false`.
ThirdParty.ENCRYPTORS[:foo]
# File activesupport/lib/active_support/message_encryptors.rb, line 126
    

rotate(**options)
rotate(&block)

options 添加到选项集列表。消息将使用列表中的第一个集进行加密。但在解密时,将按顺序尝试每个集,直到其中一个成功。

特别地,:secret_generator 选项可以指定一个与初始指定的不同的 secret 生成器。secret 生成器必须响应 call 方法,接受一个 salt 和一个 secret_length 关键字参数,并返回一个合适的 secret(字符串)或 secrets(字符串数组)。secret 生成器也可以接受其他任意关键字参数。

如果任何选项与活动 secret 生成器的关键字参数匹配,这些选项将传递给 secret 生成器而不是传递给消息加密器。

对于细粒度的每个 salt 轮换,支持块形式。块将接收 salt,并应返回一个适当的选项 Hash。块也可以返回 nil 来表示该轮换不适用于给定的 salt。例如:

encryptors = ActiveSupport::MessageEncryptors.new { ... }

encryptors.rotate do |salt|
  case salt
  when :foo
    { serializer: JSON, url_safe: true }
  when :bar
    { serializer: Marshal, url_safe: true }
  end
end

encryptors.rotate(serializer: Marshal, url_safe: false)

# Uses `serializer: JSON, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
encryptors[:foo]

# Uses `serializer: Marshal, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
encryptors[:bar]

# Uses `serializer: Marshal, url_safe: false`.
encryptors[:baz]
# File activesupport/lib/active_support/message_encryptors.rb, line 74
    

rotate_defaults

使用默认选项调用 rotate

# File activesupport/lib/active_support/message_encryptors.rb, line 154