2011-01-21 35 views
2

我想解決我的AJAX調用的安全問題。我有一個刪除註釋的jQuery post調用。從我讀過的內容來看,似乎我需要使用protect_from_forgery來確保帖子來自有效用戶。如何保護這個帖子請求到Rails?

這是我迄今爲止

application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 
... 

的index.html

$.post('../delete_note',{id:$('#note_id').val()}, function(data) { 

}); 

note_controller.rb

def delete_note 
    y params 
    render :text => "success" 
end 

在此刻,崗位請求由Rails運行,即使我沒有發送任何安全令牌。我需要做些什麼才能保證通話的安全?

我正在使用Rails 3.0.1和devise進行用戶管理。

回答

3

你可能想確保用戶登錄,使用您的筆記控制器是這樣的:

before_filter :authenticate_member!, :except => [:index] 

此外,檢查用戶是否有權刪除註釋的權利,要使用像cancan這樣的授權解決方案。

+0

感謝您的回答。如果我沒有發送任何郵寄請求,用戶如何進行身份驗證? Rails是否只有與請求一起發送的數據? – ben 2011-01-21 08:57:10