복붙노트

[RUBY-ON-RAILS] 레일에 2 응용 프로그램을 ssl_requirement를 사용하여 강제로 SSL

RUBY-ON-RAILS

레일에 2 응용 프로그램을 ssl_requirement를 사용하여 강제로 SSL

나는 SSL에서 실행해야합니다 레일 응용 프로그램이 있습니다. 나는 ssl_requirement을 시도하지만 모든 컨트롤러의 모든 행동에 입력 할 필요가 보인다.

사용자 요청이 HTTP에있을 때 앱이 자동으로 HTTPS로 리디렉션 수 있도록 내가 ssl_requirement와 응용 프로그램 컨트롤러에 before_filter를 추가 할 수있는 방법이 있습니까?

모두 감사합니다. :)

해결법

  1. ==============================

    1.랙 미들웨어를 사용합니다.

    랙 미들웨어를 사용합니다.

    # lib/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/environment.rb
    config.middleware.use "ForceSSL"
    
  2. ==============================

    2.요청이 응용 프로그램의 before_filter에서 SSL에 있거나없는 경우 테스트를 시도 할 수 있습니다

    요청이 응용 프로그램의 before_filter에서 SSL에 있거나없는 경우 테스트를 시도 할 수 있습니다

    class Application < AC::Base
    
      before_filter :need_ssl
    
      def need_ssl
        redirect_to "https://#{request.host}/#{request.query_string}" unless request.ssl?
      end
    end
    
  3. ==============================

    3.핵심 문제는로드되지 않도록 force_ssl.rb이며, 그 lib가 레일 3.1에서 기본적으로로드되지 않습니다. 당신은 추가해야

    핵심 문제는로드되지 않도록 force_ssl.rb이며, 그 lib가 레일 3.1에서 기본적으로로드되지 않습니다. 당신은 추가해야

    config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    

    application.rb에

  4. from https://stackoverflow.com/questions/3861772/force-ssl-using-ssl-requirement-in-rails-2-app by cc-by-sa and MIT license