Rails 6 - 常量 ActionController::InvalidAuthenticityToken

Ran*_*inn 9 ruby-on-rails csrf actioncontroller authenticity-token ruby-on-rails-6

我正在修改 Rails 6 并且我不断在由 rails 生成的表单上获取 ActionController::InvalidAuthenticityToken,例如(实现 rails 教程书籍注册/登录流程)

<%= form_for(@user, url: 'signup') do |f| %>
     <%= render 'partials/error_messages' %>
     <%= f.label :name, "Nimi" %>
     <%= f.text_field :name %>
     <%= f.label :email, "E-mail" %>
     <%= f.email_field :email %>
     <%= f.label :password, "Parool" %>
     <%= f.password_field :password %>
     <%= f.label :password_confirmation, "Korda parooli" %>
     <%= f.password_field :password_confirmation %>
     <%= f.submit "Loo konto", class: "button-green" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这发生在所有形式上,输出转储看起来像这样

转储

应用程序.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Storebase - kaasaegsed e-poed!</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body class="bg-gray-100 text-gray-900">
    <% flash.each do |message_type, message| %>
      <div class="bg-blue-100 text-blue-500 flex items-center h-12 px-12 shadow-lg flash-<%= message_type %>"><%= message %></div>
    <% end %>

    <%= yield %>
    <%= debug(params) if Rails.env.development? %>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

dav*_*pm4 3

在一个控制器中看到了这一点,该控制器将 Devise 子类化以获取动作挂钩。这只是发生在:destroy我的行动上。

\n\n
class MySessionsController < Devise::SessionsController\n  after_action :after_login,  only: [:create]\n  after_action :after_logout, only: [:destroy]\n\n  private\n\n  def after_login\n  end\n\n  def after_logout\n  end\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

我没有看到在销毁时跳过身份验证的风险,因此在控制器内添加这一行为我解决了这个问题:skip_before_action :verify_authenticity_token, only:[:destroy]。\xc2\xaf_(\xe3\x83\x84)_/\xc2\xaf

\n\n

这是 Rails6 升级引入的错误或功能吗?是否存在我通过跳过而未发现的安全风险?任何见解将不胜感激:)

\n

  • 我看到没有人回答,我在尝试升级到 Rails 6 时遇到了同样的问题。您是否找到过有关此问题的解释? (5认同)