重复的步骤
登录
登出
从服务器登录并获取422无法验证CSRF令牌的真实性
宝石
设计3.5.2
devise_token_auth 0.1.36
根据其他线程,解决方案是在注销时返回一个新的csrf标记,然后在成功处理程序的客户端注销,将XSRF-TOKEN的cookie设置为接收的标记 . 我正在使用的代码如下 . 有人可以告诉我为什么它不起作用?最后一次登录请求看起来正在使用新令牌,因此有点像是从cookie中提取它 .
我重写了devise_token_auth destroy方法,并在渲染中添加了csrfParam和csrfToken以传递给客户端 . 这个csrfToken是否需要存储在某个地方的服务器上,以便在下一个请求通过时进行比较?
def destroy
# remove auth instance variables so that after_filter does not run
user = remove_instance_variable(:@resource) if @resource
client_id = remove_instance_variable(:@client_id) if @client_id
remove_instance_variable(:@token) if @token
if user and client_id and user.tokens[client_id]
user.tokens.delete(client_id)
user.save!
render json: {
success:true,
csrfParam: request_forgery_protection_token,
csrfToken: form_authenticity_token
}, status: 200
else
render_destroy_error
end
end
这是ng-token-auth signOut的客户端成功回调 .
$auth.signOut()
.then(function(resp) {
$('meta[name=csrf-token]').attr('content', resp.data.csrfToken);
$cookieStore.put($http.defaults.xsrfCookieName, resp.data.csrfToken);
$http.defaults.headers.common[$http.defaults.xsrfHeaderName] = resp.data.csrfToken;
$state.go('login');
})
.catch(function(resp) {
// handle error response
console.log("error signing out");
});
我跟着下面的问题,这个问题与我的相似,但没有任何运气 . Rails, Devise authentication, CSRF issue https://github.com/lynndylanhurley/devise_token_auth/issues/398
1 回答
我遇到了同样的问题,发现这篇文章引出了几个不同的答案 . Rails, Devise authentication, CSRF issue
我用卢卡斯的答案对我有用 . 您需要做的就是将此行放在config / initializers / devise.rb中 .