在Rails 3.1中强制SSL用于特定路由
我需要在我的应用程序中的所有路由上强制使用SSL,除了landing#index
。
在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
我在这里问了一个关于stackoverflow的类似问题,并被告知使用https://github.com/tobmatth/rack-ssl-enforcer 。 我还没有尝试过,但基于自述文件,它似乎解决了在某些路线上有条件地执行ssl的问题。
使用ActiveAdmin 1.0b的Rails 4,我修改了config / initializers / active_admin.rb:
config.before_filter :force_ssl_redirect, if: :https_enabled?
force_ssl_redirect
在actionpack / lib / action_controller / metal / force_ssl.rb中定义,是Rails的force_ssl
类方法调用的。
https_enabled?
在我的application_controller.rb中定义:
def https_enabled? ENV['HTTPS_ENABLED'] == 'true' end
你可以这样做:
调节器
force_ssl :except => :index
视图
假设您的索引路径名是index_landing_path
<%= link_to 'Landing', index_landing_path, :protocol => 'http' %>