我正在使用devise
(最新版本 - 3.2.0)和rails
(最新版本 - 4.0.1)
我正在进行简单的身份验证(没有 ajax 或 api)并收到 CSRF 真实性令牌错误。检查下面的 POST 请求
started POST "/users/sign_in" for 127.0.0.1 at 2013-11-08 19:48:49 +0530
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"SJnGhXXUXjncnPhCdg3muV2GYCA8CX2LVFV78pqddD4=", "user"=>
{"email"=>"a@a.com", "password"=>"[FILTERED]", "remember_me"=>"0"},
"commit"=>"Sign in"}
Can't verify CSRF token authenticity
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."email" =
'a@a.com' LIMIT 1
(0.1ms) begin transaction
SQL (0.4ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?,
"sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 2 [["last_sign_in_at", Fri,
08 Nov 2013 14:13:56 UTC +00:00], ["current_sign_in_at", Fri, 08 Nov 2013 14:18:49 UTC
+00:00], ["sign_in_count", 3], ["updated_at", Fri, 08 Nov 2013 14:18:49 UTC +00:00]]
(143.6ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 239ms (ActiveRecord: 144.5ms | Search: 0.0ms)
根 url 指向home#new
,就像
class HomeController < ApplicationController
before_action :authenticate_user!
def index
end
end
Sign_in 页面生成的 html 视图如下:
元标记
<meta content="authenticity_token" name="csrf-param" /> <meta content="aV+d7Z55XBJF2VtyL8V3zupR3OwhaQ6UHNtlQLBQf5Y=" name="csrf-token" />
形式
<form accept-charset="UTF-8" action="/users/sign_in" class="new_user" id="new_user" method="post"> <div style="margin:0;padding:0;display:inline"> <input name="utf8" type="hidden" value="✓" /> <input name="authenticity_token" type="hidden value="aV+d7Z55XBJF2VtyL8V3zupR3OwhaQ6UHNtlQLBQf5Y=" /> </div> <div><label for="user_email">Email</label><br /> <input autofocus="autofocus" id="user_email" name="user[email]" type="email" value="" /> </div> <div><label for="user_password">Password</label><br /> <input id="user_password" name="user[password]" type="password" /></div> <div><input name="user[remember_me]" type="hidden" value="0" /> <input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1" /> <label for="user_remember_me">Remember me</label></div> <div><input name="commit" type="submit" value="Sign in" /></div> </form>
身份验证请求甚至会更新last_sign_in_at
和sign_in_count
值,但是当我尝试current_user
在控制器中访问时,它会以nil
.
据我说,它实际上并没有登录user
。但随之而来的问题是“为什么它在表中更新last_sign_in_at
/sign_in_count
值user
?”