2010-09-03 34 views
28

我正在升級應用程序到Rails 3.0.0,想知道添加SSL的標準方法是否已經改變(我隱約記得演示表明路由器現在可以處理SSL,儘管我不確定它是否會僅用於演示目的)。我目前使用「ssl_requirement」gem,但它給出:Rails 3 SSL棄用

拒絕警告:使用#request_uri已棄用。改爲使用完整路徑。 (從ensure_proper_protocol在/Library/Ruby/Gems/1.8/gems/ssl_requirement-0.1.0/lib/ssl_requirement.rb:53稱爲)

此外,它似乎打破處理新的「數據的方法時'屬性。例如:

<%= link_to "Logout", user_path, :method => :delete %> 

從所述應用的SSL部分存取時工作正常,但失敗(試圖使顯示動作)從非SSL部,再後,當(在用戶控制器的所有操作需要SSL,雖然我知道銷燬行爲不會傳輸安全數據)。

回答

46

它確實對Rails 3很簡單在config/routes.rb

MyApplication::Application.routes.draw do 
    resources :sessions, :constraints => { :protocol => "https" } 
end 

或者,如果你需要強制SSL多重路線:

MyApplication::Application.routes.draw do 
    scope :constraints => { :protocol => "https" } do 
    # All your SSL routes. 
    end 
end 

並鏈接到SSL路線可以這樣做:

<%= link_to "Logout", sessions_url(:protocol => 'https'), :method => :delete %> 

如果您希望自動重定向某些控制器(或實際上,一些子路徑)爲等效的基於HTTPS的URL,您可以添加這樣的事情你的路由(我想這部分比較簡單):

# Redirect /foos and anything starting with /foos/ to https. 
match "foos(/*path)", :to => redirect { |_, request| 
    "https://" + request.host_with_port + request.fullpath } 
+1

這似乎比使用'ssl_requirement'更復雜。它是在Rails 3中使用它的新標準方法,還是'ssl_requirement'仍然可用?謝謝。 – 2010-09-03 15:58:09

+1

@Kevin:除了自動重定向,我覺得這很容易。而且,這一切都可以通過* standard * routing DSL完成,這是Rails 2無法實現的,因此需要一個外部庫。 – molf 2010-09-03 16:11:43

+4

在開發環境中,執行 scope:constraints => {:protocol => Rails.env.production? ? 'https':'http'}做 ... 結束 來源:http://www.themomorohoax.com/2010/10/08/using-ssl-in-rails-3 – mysmallidea 2011-04-27 03:22:53

20

花一個下午的時間尋找最佳的解決方案後,我的辦法解決在這篇文章中描述:http://clearcove.ca/blog/2010/11/how-to-secure-a-rails-app-on-heroku-with-ssl-firesheep/其中引用這篇文章:Force SSL using ssl_requirement in Rails 2 app

基本上做到這一點:

# lib/middleware/force_ssl.rb 
class ForceSSL 
    def initialize(app) 
    @app = app 
    end 

    def call(env) 
    if env['HTTPS'] == 'on' || env['HTTP_X_FORWARDED_PROTO'] == 'https' 
     @app.call(env) 
    else 
     req = Rack::Request.new(env) 
     [301, { "Location" => req.url.gsub(/^http:/, "https:") }, []] 
    end 
    end 
end 

# config/application.rb 
config.autoload_paths += %W(#{ config.root }/lib/middleware) 

# config/environments/production.rb 
config.middleware.use "ForceSSL" 
+1

我更喜歡這種方法,因爲即使在開發環境中,@ molf的解決方案也需要SSL。 – 2011-01-26 18:17:33

14

Toppic是舊的,但只是使用Google的人:

在*應用程序/控制器/ your_controller.rb *

class LostPasswordsController < ApplicationController 

    force_ssl 

    def index 
    #.... 
    end 
end 

如果應用控制器全球使用它

http://apidock.com/rails/ActionController/ForceSSL/ClassMethods/force_ssl

... THX S.L.對於尖

+0

謝謝你。結合「except」和「only」選項,這是選擇性SSL的一個很好的解決方案。 – LouieGeetoo 2012-04-04 20:55:00

+1

導軌3.1只有 – 2012-05-26 03:08:03

+0

b.t.w. 'config.force_ssl'和控制器'force_ssl'之間存在差異http://www.eq8.eu/blogs/14-config-force_ssl-is-different-than-controller-force_ssl – equivalent8 2016-05-08 10:08:30

1

在以後的Rails(至少3.12+),您可以使用以下,環境特定的:

在配置/環境/ production.rb(或其他環境)

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
config.force_ssl = true