跳至內容 跳至搜尋
方法
#
C
I
O
R

屬性

[RW] transitional

如果為 true,在建立訊息驗證值時,會交換前兩個旋轉選項組。例如,使用下列設定時,訊息驗證值將使用 `serializer: Marshal, url_safe: true` 產生訊息,並能夠驗證使用三個選項組中任一個產生的訊息

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

這可能會在執行應用程式的滾動部署時有用,尚未更新的伺服器仍然必須能夠驗證已更新伺服器的訊息。在這樣的場景中,首先執行滾動部署,採用新的旋轉(例如 `serializer: JSON, url_safe: true`)作為第一個旋轉,並設 `transitional = true`。接著在所有伺服器都更新完畢後,執行第二次滾動部署,並設 `transitional = false`。

執行個體公開方法

[](salt)

傳回使用來自給定 `salt` 的機密,以及來自 rotate 的選項所設定的 MessageVerifierMessageVerifier 執行個體將會記住,因此相同的 `salt` 會傳回相同的執行個體。

# File activesupport/lib/active_support/message_verifiers.rb, line 47
    

[]=(salt, verifier)

覆寫與給定 `salt` 關聯的 MessageVerifier 執行個體。

# File activesupport/lib/active_support/message_verifiers.rb, line 55
    

clear_rotations

清除選項組串列。

# File activesupport/lib/active_support/message_verifiers.rb, line 113
    

initialize(&secret_generator)

初始化一個新的執行個體。`secret_generator` 必須接受一個 `salt`,並傳回一個合適的機密(字串)。`secret_generator` 可能也會接受任意關鍵字引數。如果呼叫 rotate 時有任何選項符合這些關鍵字引數,這些選項將會傳遞給 `secret_generator`,而不是傳遞給訊息驗證值。

verifiers = ActiveSupport::MessageVerifiers.new do |salt, base:|
  MySecretGenerator.new(base).generate(salt)
end

verifiers.rotate(base: "...")
# File activesupport/lib/active_support/message_verifiers.rb, line 31
    

on_rotation(&callback)

設定一個呼叫回,當訊息使用非第一個選項組驗證時呼叫此呼叫回。

例如,此呼叫回可在每次呼叫時記錄,並因此表示舊選項組是否仍持續使用,或可以從旋轉中移除。

# File activesupport/lib/active_support/message_verifiers.rb, line 119
    

rotate(**options)

options 新增至選項集清單中。 Messages 將會使用清單中第一組進行簽署。然而,在驗證時,每個選項集會依序嘗試,直到驗證成功。

特別注意,:secret_generator 選項可以指定不同於最初指定的密碼產生器。密碼產生器必須回應 call,接受 salt 並回傳適當的密碼 (字串)。密碼產生器也可能接受任意 kwargs。

如果任何選項符合有效密碼產生器的 kwargs,這些選項將會傳遞至密碼產生器,而不是傳遞至訊息驗證程式。

對於精細的 per-salt 輪替,支援封鎖形式。封鎖將接收 salt,並應回傳適當的選項 Hash。封鎖也可以回傳 nil 以指示輪替不套用至給定的 salt。例如

verifiers = ActiveSupport::MessageVerifiers.new { ... }

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

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

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

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

# Uses `serializer: Marshal, url_safe: false`.
verifiers[:baz]
# File activesupport/lib/active_support/message_verifiers.rb, line 61
    

rotate_defaults

使用預設選項呼叫 rotate

# File activesupport/lib/active_support/message_verifiers.rb, line 107