Ruby on rails 间歇Rails 5 ActionController::InvalidAuthenticationToken

Ruby on rails 间歇Rails 5 ActionController::InvalidAuthenticationToken,ruby-on-rails,Ruby On Rails,上下文:一个正在生产的Rails应用程序,托管在Heroku上,拥有大约800名用户 Ruby 2.4.2 轨道5.1.4 设计4.3.0 出于某种原因,我看到一些用户遇到错误: ActionController::InvalidAuthenticityToken [GEM_ROOT]/gems/actionpack-5.1.4/lib/action_controller/metal/request_forgery_protection.rb:195 对于POST/students/:i

上下文:一个正在生产的Rails应用程序,托管在Heroku上,拥有大约800名用户

  • Ruby 2.4.2
  • 轨道5.1.4
  • 设计4.3.0
出于某种原因,我看到一些用户遇到错误:

ActionController::InvalidAuthenticityToken

[GEM_ROOT]/gems/actionpack-5.1.4/lib/action_controller/metal/request_forgery_protection.rb:195
对于
POST/students/:id/registrations
的请求

它是间歇性的,很少有用户遇到这种错误

客户端是iPad上的Safari 11.0

应用程序控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!, unless: :devise_controller?
  before_action :restrict_from_students, unless: :devise_controller?
  # ...
end
class RegistrationsController < ApplicationController
  skip_before_action :restrict_from_students, only: :create
  # ...
end
class ApplicationController
注册管理员:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!, unless: :devise_controller?
  before_action :restrict_from_students, unless: :devise_controller?
  # ...
end
class RegistrationsController < ApplicationController
  skip_before_action :restrict_from_students, only: :create
  # ...
end
类注册控制器

是否有一些场景(重新发布请求、身份验证超时但提交、缺少JS)会导致这种情况?我似乎无法复制它。

多亏了橡皮鸭,我复制了这个问题

  • 注销
  • “返回”到缓存的应用程序UI
  • 单击按钮生成POST请求
  • 观察例外情况
  • 这里的解决方案是使用
    rescue\u from
    将用户重定向到登录页面


    谢谢你,橡皮鸭

    我也有类似的问题

    在应用程序控制器中使用
    rescue\u from
    ,并通过通知重定向到有用的位置。在我的例子中,我尝试将用户重定向回他们要重新尝试其操作的位置,或者将其重定向到主页作为回退

    rails 5的示例:

    class ApplicationController < ActionController::Base
      rescue_from ActionController::InvalidAuthenticityToken, 
        with: :handle_invalid_token
    
    
      def handle_invalid_token
        redirect_back fallback_location: root_path, 
          notice: 'Stale session detected'
      end
    end
    
    class ApplicationController
    如果您无法复制自己,那么很难提供帮助,您是在隐藏的表单字段中设置csrf标记,还是在html头部设置元标记?@JPSilvashy谢谢。我想我终于重现了这个错误。这是会话超时,应用程序界面仍然可见,用户发送帖子。我来补充答案,让我猜猜!您可能也在使用TurboLink?我想如果是这样的话,你的后退按钮可能也有问题。