【发布时间】:2016-03-06 11:05:13
【问题描述】:
谁能给我解释一下这行代码?
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
我应该使用它吗?为什么和为什么不。谢谢。
【问题讨论】:
标签: ruby-on-rails authentication devise token
谁能给我解释一下这行代码?
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
我应该使用它吗?为什么和为什么不。谢谢。
【问题讨论】:
标签: ruby-on-rails authentication devise token
verify_authenticity_token 是 Rails 使用的 before_action(在每个控制器操作之前调用的方法,在 Rails 4 之前称为 before_filter)防止CSRF 攻击。 You can read more about how Rails does this here.
这行代码的意思是:“如果这是一个 JSON 请求,则跳过对该控制器的 CSRF 检查”。
这对于需要提供给不在同一个域中的远程站点的 JSON API 很有用,因此会导致 CSRF 检查失败。这是安全的,只要您确保the API is being authenticated properly。但是,如果您的控制器不会被外部 Web 应用程序使用(并且您只是在自己的站点上执行 AJAX 操作),那么请不要关闭 verify_authenticity_token 检查。
【讨论】: