首页 文章

在rails 3中关闭CSRF令牌

提问于
浏览
95

我有一个rails应用程序,为iPhone应用程序提供一些api . 我希望能够简单地在资源上发布,而无需考虑获取正确的csrf令牌 . 我尝试了一些我在stackoverflow中看到的方法,但似乎它们不再适用于rails 3.感谢您帮助我 .

3 回答

  • 30

    在要禁用CSRF的控制器中,检查:

    skip_before_action :verify_authenticity_token
    

    或者除了几个方法之外的所有内容都禁用它:

    skip_before_action :verify_authenticity_token, :except => [:update, :create]
    

    或者仅禁用指定的方法:

    skip_before_action :verify_authenticity_token, :only => [:custom_auth, :update]
    

    更多信息:RoR Request Forgery Protection

  • 159

    在Rails3中,您可以在控制器中禁用特定方法的csrf标记:

    protect_from_forgery :except => :create
    
  • 104

    使用Rails 4,您现在可以选择 skip_before_action 而不是 skip_before_filter .

    # Works in Rails 4 and 5
    skip_before_action :verify_authenticity_token
    

    要么

    # Works in Rails 3 and 4 (deprecated in Rails 4 and removed in Rails 5)
    skip_before_filter :verify_authenticity_token
    

相关问题