Turn off CSRF token in rails 3

2019-01-04 23:24发布

i have a rails app that serves some apis to an iphone application. I want to be able to simply post on a resource without minding on get the correct csrf token. I tried some method that I see here in stackoverflow but it seems they no longer work on rails 3. Thank you for helping me.

3条回答
女痞
2楼-- · 2019-01-04 23:40

With Rails 4, you now have the option to write in skip_before_action instead of skip_before_filter.

# Works in Rails 4 and 5
skip_before_action :verify_authenticity_token

or

# Works in Rails 3 and 4 (deprecated in Rails 4 and removed in Rails 5)
skip_before_filter :verify_authenticity_token
查看更多
小情绪 Triste *
3楼-- · 2019-01-05 00:01

In Rails3 you can disable the csrf token in your controller for particular methods:

protect_from_forgery :except => :create 
查看更多
唯我独甜
4楼-- · 2019-01-05 00:06

In the controller where you want to disable CSRF the check:

skip_before_action :verify_authenticity_token

Or to disable it for everything except a few methods:

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

Or to disable only specified methods:

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

More info: RoR Request Forgery Protection

查看更多
登录 后发表回答