繁体   English   中英

Rails 5.2 ActionController::InvalidAuthenticityToken

[英]Rails 5.2 ActionController::InvalidAuthenticityToken

升级到 Rails 5.2 后,我开始收到应用程序中所有表单的ActionController::InvalidAuthenticityToken错误。 我通过禁用所有表单的 Turbolinks 来解决这个问题,这有效但不是一个很好的解决方案。

搜索互联网,推荐的常见解决方案似乎是禁用protect_from_forgery

skip_before_action :verify_authenticity_token, raise: false

为什么我们需要禁用protect_from_forgery ,这不会造成安全漏洞吗?

编辑

csrf_meta_tags在布局中。

在此处输入图片说明

对于 Rails 5.2 default_protect_from_forgery 默认ActionController::Base启用

您可以使用以下语法禁用它,如PR 中所述

config.action_controller.default_protect_from_forgery = false

参考文档

config.action_controller.default_protect_from_forgery 决定是否在 ActionController:Base 上添加伪造保护。 默认情况下这是 false,但在加载 Rails 5.2 的默认值时启用。

对于 rails 5.2,我必须将这些添加到控制器内的行中,我遇到了一个问题:

  skip_before_action :verify_authenticity_token, raise: false
  skip_after_action :verify_authorized

它适用于我的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM