2012-05-15 48 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