2012-03-02 23 views
7

相關:Rails 3 SSL routing redirects from https to http(不幸沒有工作)。我應該如何使所有Devise路徑使用https?

複製,但得到的答覆並沒有爲我工作:setting up ssl on devise

我有一個已經工作了一段時間,現在罰款的web應用程序,但我需要添加SSL登錄/編輯ACCT路徑。我正在使用Devise進行身份驗證。我在devise wiki中發現了一個條目,使得這個過程看起來非常簡單,但是如果我能使它工作,該死的。簡單的部分是這樣的:

#in config/environments/production.rb 
config.to_prepare { Devise::SessionsController.force_ssl } 
config.to_prepare { Devise::RegistrationsController.force_ssl } 

然後還有約25行代碼,在這個要點:https://gist.github.com/1040964

我得到的工作做得不夠好,但是當過我登出我從拿到301會話DELETE將我發送到GET的操作。

Started DELETE "https://stackoverflow.com/users/sign_out" for 98.246.164.160 at 2012-03-02 01:45:42 +0000 
[02 Mar 01:45 10886 INFO] Processing by Devise::SessionsController#destroy as HTML 
[02 Mar 01:45 10886 INFO] Parameters: {"authenticity_token"=>"fI4VZ4V0Go2Civo3sJz8Dv5/Wtaa90ynaYr+xxx="} 
[02 Mar 01:45 10886 DEBUG] Parameters: {"_method"=>"delete", "authenticity_token"=>"fI4VZ4V0Go2Civo3sJz8Dv5/Wtaa90ynaYr+xxxx=", "action"=>"destroy", "controller"=>"devise/sessions"} 
[02 Mar 01:45 10886 INFO] Redirected to https://ec2-xx-xx-106-255.us-west-2.compute.amazonaws.com/users/sign_out 
[02 Mar 01:45 10886 INFO] Completed 301 Moved Permanently in 3ms 

Started GET "https://stackoverflow.com/users/sign_out" for xx.xx.164.160 at 2012-03-02 01:45:42 +0000 
[02 Mar 01:45 10886 FATAL] 
ActionController::RoutingError (No route matches [GET] "https://stackoverflow.com/users/sign_out"): 

所以我覺得我需要從頭開始。使任何Devise路徑使用https最簡單的方法是什麼,但我的應用程序中的其餘路徑使用http?我試過這個(來自SO頂部的帖子):

#devise routes 
    scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do 
    devise_for :users, :controllers => { :registrations => :registrations } 
    devise_for :admins 
    end 

但是不行。我需要一個更好的建議。但

回答

10

沒有答案,所以這裏是我的結論是:

  1. 一旦你通過HTTPS訪問網站時,不要通過HTTP訪問,直到用戶退出(firesheep攻擊)。在上面鏈接的文章中,Devise中有很多內容只討論了登錄/註銷頁面上的https。餿主意。

  2. 您真正需要的是這樣的:

    #in config/environments/production.rb 
    config.to_prepare { Devise::SessionsController.force_ssl } 
    config.to_prepare { Devise::RegistrationsController.force_ssl } 
    
  3. 我有一噸的周圍,從制定 'after_sign_in_path' 的問題。事實證明,after_sign_out_path_for正在等待返回的路徑 - 這不是一個事件,而是詢問用戶應該在哪裏定向。所以我回來了root_path :protocol => 'http://'和照顧它。

2

嘗試加入讓你整個應用程序使用HTTPS:

#in config/environments/production.rb 
config.force_ssl = true 

我有不少相同的問題。有時我會退出,有時我從DELETE動作中獲得了301並重定向到了GET。對我來說,這是問題所在。

3

確保您在所有Devise鏈接中使用https(這可避免force_ssl重定向)。

在你的routes.rb(僅在生產環境中使用):

scope defaults: (Rails.env.production? ? { protocol: 'https' } : {}) do 
    devise_for :users 
end 
在應用程序中使用

現在:

destroy_user_session_url # use _url instead of _path so the protocol is added! 

現在你的註銷/登出鏈接(和其他色器件鏈接)將直接指向https。避免了從HTTP DELETE到HTTPS GET的force_ssl重寫。這一切工作:)

相關問題