2012-05-15 47 views
11

我需要強制SSL在上的所有路由在我的應用中除了landing#index爲Rails 3.1中的特定路由強制使用SSL

config/application.rb,我有:

config.force_ssl = true 

然後在landing_controller.rb,我有:

force_ssl :except => :index 

然而,所有的路由仍然被重定向到https

有誰知道如何在Rails 3.1+應用程序中有條件地強制使用SSL?

解決方案:

以下內容添加到您的Gemfile

gem 'rack-ssl-enforcer' 

以下內容添加到您的config/application.rb

config.middleware.use Rack::SslEnforcer, :except => [ /\/$/ ], :strict => true 

回答

13

我問過類似的問題在計算器here,被告知使用https://github.com/tobmatth/rack-ssl-enforcer。我還沒有嘗試過,但基於自述文件,它似乎解決了在某些路由上有條件地執行ssl的問題。

+1

乾杯!在'config/application.rb'中修改如下代碼:'config.middleware.use Rack :: SslEnforcer,:except => [/ \/$ /],:strict => true' –

-2

你可以這樣來做:

控制器

force_ssl :except => :index 

視圖

假設你的索引路徑名index_landing_path

<%= link_to 'Landing', index_landing_path, :protocol => 'http' %> 
+1

我已經試過了方法,但所有請求仍然重定向到'https'。我加了'force_ssl:except =>:index'給我的控制器無濟於事。 –

6

導軌4與ActiveAdmin 1.0B,我修改配置/初始化/ active_admin.rb:

config.before_filter :force_ssl_redirect, if: :https_enabled? 

force_ssl_redirect在ActionPack的/ LIB/action_controller /金屬/ force_ssl.rb定義,並且是什麼Rails的force_ssl類方法調用。

https_enabled?在我application_controller.rb定義:

def https_enabled? 
    ENV['HTTPS_ENABLED'] == 'true' 
end