【问题标题】:Force SSL for specific routes in Rails 3.1在 Rails 3.1 中为特定路由强制 SSL
【发布时间】:2012-05-15 16:52:40
【问题描述】:

我需要在我的应用程序中的所有路由上强制 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

【问题讨论】:

    标签: ruby-on-rails ruby ssl ruby-on-rails-3.1


    【解决方案1】:

    我在 stackoverflow here 上提出了类似的问题,并被告知使用 https://github.com/tobmatth/rack-ssl-enforcer。我还没有尝试过,但根据自述文件,它似乎可以解决您在某些路由上有条件地强制执行 ssl 的问题。

    【讨论】:

    • 干杯!用config/application.rb 中的以下行修复:config.middleware.use Rack::SslEnforcer, :except => [ /\/$/ ], :strict => true
    【解决方案2】:

    带有 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
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      控制器

      force_ssl :except => :index
      

      查看

      假设您的索引路径名称是 index_landing_path

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

      【讨论】:

      • 我已经尝试过这种方法,但所有请求仍然重定向到https。我将force_ssl :except =&gt; :index 添加到我的控制器无济于事。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      相关资源
      最近更新 更多