![](/img/trans.png)
[英]Devise error - ActionController::InvalidAuthenticityToken
[英]Getting ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken) error for devise sign in and sign up
我知道有太多与此错误相关的帖子,但仍然...
登录/注册进行设计时出现以下错误。 (两天前它可以正常工作,可以唱歌。)
Feb 02 22:16:12 myapp app/web.1: Processing by Devise::SessionsController#create as HTML
Feb 02 22:16:12 myapp app/web.1: Parameters: {"utf8"=>"✓", "authenticity_token"=>"d0R529vsmCovMKZk1RC9ioxfVHivvVGKxPFvNkEUqVId08qPMDRN0lu9yULIAaTJR+p1oOXyg8QsE+PdZx4CHg==", "user"=>{"app_name"=>"tempo", "email"=>"xyz@vision.com", "password"=>"[FILTERED]"}, "commit"=>"submit"}
Feb 02 22:16:12 myapp app/web.1: Can't verify CSRF token authenticity
Feb 02 22:16:12 myapp app/web.1: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Feb 02 22:16:12 myapp app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
Feb 02 22:16:12 myapp app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'
Feb 02 22:16:12 myapp app/web.1: vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/controllers/helpers.rb:257:in `handle_unverified_request'
Feb 02 22:16:12 myapp app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_controller/metal/request_forgery_protection.rb:204:in `verify_authenticity_token'
我在Heroku上托管的生产应用程序遇到此错误。
我有一个暂存应用程序(它也托管在heroku上,并且具有相同的代码库),并且运行良好。
我可以登录并注册。
我已经引用了以下链接,但遇到了同样的问题
Rails Facebook应用返回422“您想要的更改被拒绝”错误
设计用户sign_in给出CSRF令牌真实性令牌的身份验证错误
rails-“警告:无法验证CSRF令牌的真实性”用于json devise请求
https://github.com/plataformatec/devise/issues/2734
我使用相同的rails 4。
编辑
我已经厌倦了以下解决方案
protect_from_forgery
csrf_meta_tags
,并且存在。 authenticity_token
。 您可以在日志跟踪中找到我提交的表单详细信息。 在应用程序控制器中添加了以下内容
skip_before_filter:verify_authenticity_token,如果::devise_controller?
这是网络问题。 我们的网络管理员对防火墙进行的更改很少,这就是导致此问题的原因。 它也会影响其他站点
这可能是两件事...
1.您忘记在布局文件中添加<%= csrf_meta_tags %>
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
2.您需要将protect_from_forgery设置为以下内容:
简单地声明不带:with参数的protect_from_forgery将默认使用:null_session:
protect_from_forgery # Same as above
3. Devise的作者建议在引发此异常的特定控制器操作上禁用protect_from_forgery:
# app/controllers/users/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token, :only => :create
end
4.您需要在表单中添加以下行
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.