警告:无法使用设计验证CSRF令牌的真实性

时间:2013-06-20 07:51:45

标签: ruby-on-rails devise csrf

使用设备进行日志记录时,我收到警告:无法验证CSRF令牌真实性和401未经授权的消息。即使有效用户,它也会重定向回登录页面。

我尝试了很多解决方案,但仍无法解决问题。 我检查了我的params并且发送了应该引发的authencity令牌警告:无法验证CSRF令牌真实性问题。

这是我的日志文件

Started POST "/admin/sign_in" for 127.0.0.1 at 2013-06-20 13:12:27 +0545
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YWAa7xOf+u0ButjnaE/kz5RmL3SZKFs
x51pFbMxDqiw=", "admin"=>{"email"=>"shobhab@gmail.com", "password"=>"[FILTERED]"}}

WARNING: Can't verify CSRF token authenticity

Admin Load (0.7ms)  SELECT `admins`.* FROM `admins` WHERE `admins`.`email` = 'shobhab@gm                          ail.com' LIMIT 1
(0.3ms)  BEGIN
(0.6ms)  UPDATE `admins` SET `last_sign_in_at` = '2013-06-20 07:27:20', `current_sign_i                          n_at` = '2013-06-20 07:27:28', `sign_in_count` = 37, `updated_at` = '2013-06-20 07:27:28' WHERE `admins`.`id` =      4
(19.0ms)  COMMIT
Redirected to home page
Completed 302 Found in 179ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2013-06-20 13:12:28 +0545
Processing by Admin::DashboardController#index as HTML
Completed 401 Unauthorized in 1ms

提前致谢

这是由设计生成的表单

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:id => "login_    form"}) do |f| %>
     <h2>Log in to your account:</h2>
     <%= f.label :email, 'Email:' %>
     <%= f.text_field :email, :class => "login" %><br /><br />
    <%= f.label :password, 'Password:' %>
    <%= f.password_field :password, :class => "login" %><br /><br />
    <div class="pagination-right">
  <%= f.submit "sign in"%>
    <%= link_to "Forgot Password?", new_admin_password_path %>
    </div>
    <p class="hr">Don't have an account?</p>
    <p>Please contact your Admin or contact Talintel Support</p>
    <% end %>

在隐藏字段中,它生成真实性令牌

<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="6yM4gkyWC94ej1X2Wzn/l17xTpIymsx9wCEuw5aOJbQ=" name="authenticity_token">
</div>

1 个答案:

答案 0 :(得分:0)

今天我遇到了从2.1升级到2.2.8并且怀疑这种变化 https://github.com/plataformatec/devise/commit/415fa2ad210f37bd264e5653440413dfd90f5170

所以,我在devise / hooks / csrf_cleaner中设置了一个断点,以找出它的调用位置。我有一个before_filter正在检查current_user,它正在根据会话记录用户,从而清除令牌。

HTH。