跳至內容 跳至搜尋

Action View CSRF 輔助模組

方法
C

執行個體公開方法

csrf_meta_tag()

針對後向相容性。

別名為: csrf_meta_tags

csrf_meta_tags()

傳回含有跨網站請求偽造保護參數及令牌名稱的 meta 標籤「csrf-param」和「csrf-token」。

<head>
  <%= csrf_meta_tags %>
</head>

這些用來產生透過 :method 執行非遠端連結的動態表單。

您不需要對一般表單使用這些標籤,因為它們會產生自己的隱藏欄位。

針對 GET 以外的 Ajax 要求,從 meta 標籤中取出「csrf-token」並作為 X-CSRF-Token HTTP 標頭傳送。

別名如下: csrf_meta_tag
# File actionview/lib/action_view/helpers/csrf_helper.rb, line 22
def csrf_meta_tags
  if defined?(protect_against_forgery?) && protect_against_forgery?
    [
      tag("meta", name: "csrf-param", content: request_forgery_protection_token),
      tag("meta", name: "csrf-token", content: form_authenticity_token)
    ].join("\n").html_safe
  end
end