2015-11-24 52 views
1

步驟重複角註銷後無法驗證CSRF令牌真實性

登錄
退出
登錄,並從服務器422無法驗證CSRF令牌真實性

寶石
色器件3.5.2
devise_token_auth 0.1.36

根據其他線程,解決方案是在註銷時返回一個新的csrf令牌,然後在客戶端返回註銷成功處理程序將XSRF-TOKEN的cookie設置爲收到的令牌。我使用的代碼如下。有人能告訴我爲什麼它不起作用嗎?最後一次登錄請求看起來正在使用新的令牌,所以角度看起來像是從cookie中選取它。

我重寫了devise_token_auth銷燬方法,並在渲染中添加了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中。

config.sign_out_all_scopes = false 
+0

太棒了!這有點令人頭疼。 –

相關問題