【问题标题】:Base protocol (https://) for every render/redirect_to call每个 render/redirect_to 调用的基本协议 (https://)
【发布时间】:2012-05-14 09:20:27
【问题描述】:

有没有办法设置一个基本协议来使用

render :action => "myaction"
redirect_to :action => "myaction"

而不是调用

render :action => "myaction", :protocol => "https://"
redirect_to :action => "myaction", :protocol => "https://"

每次?

【问题讨论】:

    标签: ruby https protocols render redirecttoaction


    【解决方案1】:

    只需在您的环境配置中使用 config.force_ssl = true。

    # config/application.rb
    module MyApp
      class Application < Rails::Application
        config.force_ssl = true
      end
    end
    

    您还可以根据当前的 Rails 环境选择性地启用 https。例如,您可能希望在开发时关闭 HTTPS,并在暂存/生产时启用它。

    # config/application.rb
    module MyApp
      class Application < Rails::Application
        config.force_ssl = false
      end
    end
    
    # config/environments/production.rb
    MyApp::Application.configure do
      config.force_ssl = true
    end
    

    在幕后,Rails 将出色的 Rack::SSL Rack 中间件添加到您的应用程序中间件堆栈中。 Rack::SSL 自动过滤请求,将非 HTTPS 请求重定向到相应的 HTTPS 路径,并应用一些额外的改进来确保您的 HTTPS 请求是安全的。

    【讨论】:

    • @mzjn 感谢您的评论。我只是相应地更新了答案。继续努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2013-07-12
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多