Rails 3 SSL Deprecation

后端 未结 4 862
感动是毒
感动是毒 2020-11-27 10:13

I am upgrading an application to Rails 3.0.0 and am wondering if the standard method for adding SSL has changed (I vaguely remember demos indicating the router could now han

4条回答
  •  我在风中等你
    2020-11-27 10:40

    After spending an afternoon looking for the best solution I settled on the approach described in this article: http://clearcove.ca/blog/2010/11/how-to-secure-a-rails-app-on-heroku-with-ssl-firesheep/ which referenced this article: Force SSL using ssl_requirement in Rails 2 app

    Basically do this:

    # lib/middleware/force_ssl.rb
    class ForceSSL
      def initialize(app)
        @app = app
      end
    
      def call(env)
        if env['HTTPS'] == 'on' || env['HTTP_X_FORWARDED_PROTO'] == 'https'
          @app.call(env)
        else
          req = Rack::Request.new(env)
          [301, { "Location" => req.url.gsub(/^http:/, "https:") }, []]
        end
      end
    end
    
    # config/application.rb
    config.autoload_paths += %W( #{ config.root }/lib/middleware )
    
    # config/environments/production.rb
    config.middleware.use "ForceSSL"
    

提交回复
热议问题