【问题标题】:ActionController::InvalidAuthenticityToken in Devise::SessionsController#create error设计中的 ActionController::InvalidAuthenticityToken::SessionsController#create 错误
【发布时间】:2015-07-20 16:59:23
【问题描述】:

我在登录时遇到了以前没有的问题。直到今天,我能够毫无问题地登录。突然,当我尝试登录时,我得到:

ActionController::InvalidAuthenticityToken
def handle_unverified_request
  raise ActionController::InvalidAuthenticityToken
end

这是开发日志:

Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"kLm2udi5jHmIQbva2h1HH9IExxFo1qt/p1u9UTjtBQaeHr/23gcrSfTyJdRvhIKYJWh/MyUbSY2zf0vHU8qA9A==", "user"=>{"first_name"=>"John", "last_name"=>"Mack", "username"=>"johnmack", "email"=>"john@mack.com", "password"=>"[FILTERED]", "avatar_cache"=>""}, "commit"=>"Sign up"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms)

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  actionpack (4.2.1) lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
  actionpack (4.2.1) lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'
  devise (3.4.1) lib/devise/controllers/helpers.rb:251:in `handle_unverified_request'
  actionpack (4.2.1) lib/action_controller/metal/request_forgery_protection.rb:204:in `verify_authenticity_token'

直到几个小时前,我登录和注销都没有问题。我试过清除浏览器数据,从多个浏览器登录,重新启动服务器,删除并重新创建数据库,并将我的代码恢复到两天前的状态。我得到同样的错误。

我正在使用 Rails 4.2 和 Devise 3。

这里有什么我遗漏的吗?

谢谢。

【问题讨论】:

  • 是的,您缺少 CSRF 令牌。看来您正在使用 ajax 请求或构建非 Rails 表单。

标签: ruby-on-rails devise


【解决方案1】:

我最终将问题追溯到我如何设置会话存储以启用跨子域身份验证。原来我有这个:

HeartbeatPods::Application.config.session_store :cookie_store, key: '_Heartbeat-pods_session', domain: ENV["HEARTBEAT_HOSTNAME"], tld_length: 2

我改成这样了:

if Rails.env.production?
    Rails.application.config.session_store :cookie_store, key: '_Heartbeat-pods_session', domain: :all
else
    Rails.application.config.session_store :cookie_store, key: '_Heartbeat-pods_session'
end

最初的解决方案在我的本地系统上支持跨子域身份验证,但最终失败了。更新后的解决方案仅支持生产设置上的跨子域身份验证。

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 2014-12-17
    • 2016-05-12
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多