【问题标题】:SSL with Ruby on RailsSSL 与 Ruby on Rails
【发布时间】:2011-01-08 11:08:50
【问题描述】:

我需要做些什么才能让我的 ruby​​ on rails 应用程序的流量使用 https?我安装了证书,如果我在访问站点时在地址栏中手动输入“https://”,则会出现小锁图标,但只需在浏览器中手动访问 www.example-app.com 即可通过 http 发送流量://。

是否有一些单行配置或者比这更复杂?我以前从未处理过 SSL,所以如果我听起来我不知道发生了什么,请原谅。

我在 (gs) 的 MediaTemple 主持,如果这很重要或有人有这种设置的经验。

【问题讨论】:

    标签: ruby-on-rails ssl https


    【解决方案1】:

    查看ssl_requirement gem。

    它允许您在控制器中指定应该通过 https 提供哪些操作,以及哪些操作可以通过 https 提供。然后它将负责从 http 重定向到 https,反之亦然。

    来自文档:

    class ApplicationController < ActiveRecord::Base
      include SslRequirement
    end
    
    class AccountController < ApplicationController
      ssl_required :signup, :payment
      ssl_allowed :index
    
      def signup
        # Non-SSL access will be redirected to SSL
      end
    
      def payment
        # Non-SSL access will be redirected to SSL
      end
    
      def index
        # This action will work either with or without SSL
      end
    
      def other
        # SSL access will be redirected to non-SSL
      end
    end
    

    【讨论】:

    • 是的,看起来知识库确实发生了变化,不是吗。我会删除它。
    【解决方案2】:

    Ruby on Rails 是一个应用程序框架,而不是一个 Web 服务器。您需要更改的 HTTPS 配置位于您的 Web 服务器(Apache、nginx 等)配置中。

    【讨论】:

    • 好吧,我知道这么多。我想我很好奇如何去改变那个设置。那会在哪里?至少它是一个研究负责人。我会研究一下 apache 和 mongrel 配置文件。
    • 你不需要换杂种。只有 Apache 配置文件。我认为默认配置文件包含 HTTPS 配置,但默认情况下它是禁用的。还可以考虑使用 nginx 网络服务器(使用更少的资源,更容易配置)。
    【解决方案3】:

    这很容易,而且您不需要宝石。我在rails here 中写了如何在没有www 的情况下重定向。重定向到https (几乎)完全一样。

    class ApplicationController < ActionController::Base
      before_filter :redirect_to_https
    
      def redirect_to_https
        redirect_to "https://example.com#{request.fullpath}" if !request.ssl? && request.host != "localhost"
      end
    end
    

    将您的 before_filter 应用到您想要确保保持 SSL 安全性的任何内容上。我通常是代码重用和 gems 的一个,但这一个非常简单。阅读有关request.protocol 的更多信息。 (注意在 Ruby 1.9.3 / Rails 3.2 环境中,名称是request.fullpath;在一些早期版本中,它是request.request_uri;参见发行说明等)

    【讨论】:

    • 这不会在开发环境中引起问题吗?除非你也抽象出 example.com 部分?
    • request.request_uri 是否更改了方法名称?它没有在我的 Ruby 1.9.3 / Rails 3.2 环境中定义。
    • request.request_uri => request.fullpath -- rtfreleasenotes.
    【解决方案4】:

    https://github.com/bartt/ssl_requirement 这是ssl_requirement 的更新版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 2011-01-21
      • 2011-11-01
      • 2012-04-20
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      相关资源
      最近更新 更多