以下是由我的 Rails 应用程序中的表单引起的错误:
Processing UsersController#update (for **ip** at 2010-07-29 10:52:27) [PUT]
Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"ysiDvO5s7qhJQrnlSR2+f8jF1gxdB7T9I2ydxpRlSSk=", **more parameters**}
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
每个非 get
请求都会发生这种情况,并且如您所见,authenticity_token
就在那里。
最佳答案
我遇到了同样的问题,但是页面已缓存。页面使用陈旧的真实性 token 进行缓冲,并且使用 post/put/delete 方法的所有操作都被识别为伪造尝试。错误(422 无法处理的实体)已返回给用户。
<小时/>Rails 3 的解决方案:
添加:
skip_before_filter :verify_authenticity_token
<小时/>
或如 sagivo和 barlop Rails 4 和 5 中指出:
添加
skip_before_action :verify_authenticity_token
在进行缓存的页面上。
<小时/>由 barlop 添加的注释关于Rails 5.2:
它弃用了 skip_before_filter
,转而使用 skip_before_action
。
考虑this Q/A或this official RoR doc .
<小时/>The *_filter family of methods have been removed from the documentation. Their usage is discouraged in favor of the *_action family of methods
对于Rails 6(如collimarco指出)
您可以使用 skip_forgery_protection
并且将其用于不使用 session 数据的 REST API 是安全的。
如@toobulkeh评论说,这不是 :index
、:show
操作上的漏洞,但要小心在 :put
上使用此漏洞, :post
操作。
例如:
caches_page :index, :show
skip_before_filter :verify_authenticity_token, :only => [:index, :show]
关于ruby-on-rails - ActionController::InvalidAuthenticityToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3364492/