如何仅针对 json 关闭 Rails Protect_from_forgery 过滤器

发布于 2024-11-02 13:38:12 字数 1058 浏览 1 评论 0原文

我有使用 Rails3 构建的网站,现在我想实现 json API 以用于移动客户端访问。但是,由于 protected_from_forgery 过滤器,从客户端发送 json post 请求。因为客户端不会从服务器检索任何数据,所以客户端无法接收 auth_token 所以我想仅针对 json 请求关闭 protected_from_forgery 选项(我认为rails3默认会这样做,但显然它没有) 。

我知道此处讨论了类似的主题,但在这种情况下,他会收到 auth_token在发送 post 请求之前。

所以我的问题是仅针对 json 关闭 protected_from_forgery 是这样做的好方法吗?如果是,该怎么做?如果不是,还有什么替代方案?

仅供参考,我使用以下 ajax 请求

$.ajax({
             type: 'POST',  
             url: "http://www.example.com/login.json",  
             data: { 'email': emailVal, 'password': passwordVal },  
             success: onSuccess,  
             error: onError,  
             dataType: "json"  
});

,并收到 ActionController::InvalidAuthenticityToken 错误。

顺便说一下,下面的curl命令是有效的......

curl -H "Content-Type: application/json" -c cookies.txt -d '{"email": emailVal, "password": passwordVal}' -X POST http://www.example.com/login.json -i

I have web site built with Rails3 and now I want to implement json API for mobile client access. However, sending json post request from the client because of the protect_from_forgery filter. Because the client will not retrieve any data from the server, there is no way that the client can receive auth_token so I would like to turn off the protect_from_forgery option only for json requests (I thought rails3 does this in default but apparently it does not).

I know similar topic is discussed at here but in that case, he receives auth_token before sending post request.

So my question is turning off the protect_from_forgery only for json is good way of doing this? If yes, how to do so? If no, what is the alternative?

FYI, I use following ajax request

$.ajax({
             type: 'POST',  
             url: "http://www.example.com/login.json",  
             data: { 'email': emailVal, 'password': passwordVal },  
             success: onSuccess,  
             error: onError,  
             dataType: "json"  
});

and I get ActionController::InvalidAuthenticityToken error.

By the way, following curl command works though...

curl -H "Content-Type: application/json" -c cookies.txt -d '{"email": emailVal, "password": passwordVal}' -X POST http://www.example.com/login.json -i

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写给空气的情书 2024-11-09 13:38:13

您可以在表单中传递authenticity_token字段,而不是禁用CSRF检查,例如:

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>

http://apidock.com/rails/v2.0.0/ActionController/RequestForgeryProtection/ClassMethods/protect_from_forgery

Instead of disabling the CSRF check you can pass the authenticity_token field in your forms, eg:

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>

http://apidock.com/rails/v2.0.0/ActionController/RequestForgeryProtection/ClassMethods/protect_from_forgery

杀お生予夺 2024-11-09 13:38:12

如果是 json 请求,您可以跳过真实性令牌检查

class ApplicationController < ActionController::Base
  skip_before_filter :verify_authenticity_token, if: :json_request?

  def json_request?
    request.format.json?
  end
end

You can just skip the authenticity token check if its a json request

class ApplicationController < ActionController::Base
  skip_before_filter :verify_authenticity_token, if: :json_request?

  def json_request?
    request.format.json?
  end
end
执着的年纪 2024-11-09 13:38:12

将以下代码添加到您的 ./app/controllers/application_controller.rb 中:

protect_from_forgery unless: -> { request.format.json? }

Add the code below to your ./app/controllers/application_controller.rb:

protect_from_forgery unless: -> { request.format.json? }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文