2017-01-18 7 views
1

私のプロダクションサーバでランダムなActionController::InvalidAuthenticityTokenエラーが発生します。 AJAXリクエストはありません。すべてのフォームはRailsヘルパーによって作成されます(したがって、authenticity_tokenで隠されたフィールドを含みます)。ランダムActionController :: InvalidAuthenticityToken

この問題はランダムな形でランダムに表示されます。

私のApplicationControllerには、​​3210があります。それが問題なのでしょうか?私はこれを追加しました。なぜなら私のユーザーはしばしばブラウザで「戻る」をクリックして、キャッシュされた結果を望んでいないからです。

マイsession_store.rb初期化子は、次のようになります。それは私がtimeoutableモジュールと1.hourへのconfig.timeout_inセットで工夫を使用するすべての違いを作る場合

Rails.application.config.session_store :cookie_store, key: '_myapp_session' 

私はRails 5.0.1を使用しています。

application_controller.rb:そのあなたのbefore_actionうん

class ApplicationController < ActionController::Base 
    include Pundit 
    rescue_from Pundit::NotAuthorizedError, with: :not_authorized 
    protect_from_forgery with: :exception 
    add_flash_types :success, :warning, :danger, :info 
    before_action :authenticate_user! 
    before_action :set_current_user 
    before_action :set_cache_headers 

    def after_sign_out_path_for(_user) 
    tasks_path 
    end 

    def not_authorized 
    flash[:warning] = 'You are not authorized to execute this action.' 
    redirect_to tasks_path 
    end 

    private 

    def set_current_user 
    User.current = current_user 
    end 

    def set_cache_headers 
    response.headers['Cache-Control'] = 'no-cache, no-store' 
    response.headers['Pragma'] = 'no-cache' 
    response.headers['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT' 
    end 
end 

答えて

0

:問題が発生しexpires_now。来るすべてのリクエストがユーザーセッションを失効させるためです。

何ができるかという

これはあなたのバックボタンの問題を解決します

class ApplicationController < ActionController::Base 
    before_action :set_cache_headers 

    private 

     def set_cache_headers 
     response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" 
     response.headers["Pragma"] = "no-cache" 
     response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT" 
     end 
    end 

です。

もう1つの問題は、1時間のアイドル時間が経過した後、ユーザーセッションがタイムアウトしてしまうことです。タイムアウトすると、サーバー側でログアウトされますが、クライアント側では、この時点でセッションのタイムアウト/ログアウトについてのヒントは得られません。したがって、問題の解決策は、タイムアウトについてクライアント側に通知し、サインインページにリダイレクトすることです。

詳細については、automatic logout after inactivity/idleを確認してください。

+0

適切な解決策に見える場合は、許容可能とマークしてください。 –

+0

いいえ、これは役に立ちません。 'ActionController :: InvalidAuthenticityToken'はまだ時々発生します。 – pmichna

+0

あなたのレイアウトファイルに '<%= csrf_meta_tags%>'があるかどうかを調べることができますそれ以外の場合はアプリケーションコントローラコードも貼り付けることができますか? –

関連する問題