[英]skip_before_filter :authenticate_user! does not work for AJAX call
[英]Is it safe to escape authenticity token using skip_before_filter?
Button_to
以其形式產生真實性令牌。 但是,會引發無效的真實性令牌錯誤,因此許多線程建議將回調添加到目標控制器中的特定操作,如下所示:
<!-- view filw -->
<%= button_to "Delete all", "", :method => :delete, :id => "delete_confirmation_button",:onclick => "return confirm('Are you sure you want to delete items?')",
:params => {"#{object.class.name.downcase}_id" => object.id },
:form => {:id => "tool_bar_delete_form"} %>
由於表單路徑是根據所選項目決定的,因此我使用以下函數在以后添加表單操作
function detect_selected_items(object_name){
/* some code */
var items_ids_string = "" ;
switch (object_name )
{
case "Questionnaire":
items_ids_string = "/questionnaire_items/" ;
break;
case "Lesson":
items_ids_string = "/lesson_items/" ;
break;
case "Exam":
items_ids_string = "/answers/" ;
break;
};
items_ids_string += "delete_all/"
items_ids_string += items_ids.join(",")
document.getElementById("tool_bar_delete_form").action = items_ids_string ;
}
<!-- controller file -->
skip_before_filter :verify_authenticity_token, :only => :delete_all
因為回調效果很好。 我的問題:
將此類回調函數添加到控制器是否安全? 在這種情況下如何預防CSRF?
通過腳本添加表單操作是否會導致此類錯誤?
不,在這種情況下,您不應跳過CSRF保護。 您是否有JavaScript攔截了delete_confirmation_button
的點擊? 我幾乎可以肯定您會這樣做,不受阻的button_to
不會有此問題。
如果要通過JS截獲點擊並使用JS提交表單,則應更新腳本以包括頁面上存在的CSRF meta
標記的內容。 CSRF元標記如下所示:
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="include-this-in-your-request" />
將名稱為csrf-token
的參數添加到名為“ authenticity_token”的請求中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.