Rails 3 SSL routing redirects from https to http

前端 未结 2 1082

This question relates to this SO question and answer (rails-3-ssl-deprecation ) where its suggested to handle ssl in rails 3 using routes.rb and routes like:



        
相关标签:
2条回答
  • 2020-11-30 20:54

    This was a long time ago and I'm sure it can be improved, but back on some old version of rails I had this code in application controller. Not sure this is still valid for Rails 3, but it may be of some help:

    private
      SECURE_ACTIONS = {
        :login => ["login", "login_customer", "remind_password", "add_customer", "add_or_login_customer"], 
        :store => ["checkout", "save_order"],
        :order => ["show"] }
    
      # Called as a before_filter in controllers that have some https:// actions
      def require_ssl
        unless ENV['RAILS_ENV'] != 'production' or  @request.ssl?
          redirect_to :protocol => 'https://', :action => action_name
          # we don't want to continue with the action, so return false from the filter
          return false
        end
      end
    
    def default_url_options(options)
        defaults = {}    
    
        if USE_EXPLICIT_HOST_IN_ALL_LINKS
          # This will OVERRIDE only_path => true, not just set the default.
          options[:only_path] = false
          # Now set the default protocol appropriately:
          if actions = SECURE_ACTIONS[ (options[:controller] || controller_name).to_sym ] and 
             actions.include? options[:action]
    
            defaults[:protocol] = 'https://'
            defaults[:host] = SECURE_SERVER if defined? SECURE_SERVER
          else
            defaults[:protocol] = 'http://'
            defaults[:host] = NON_SECURE_SERVER if defined? NON_SECURE_SERVER
          end
        end
        return defaults
      end
    

    The USE_EXPLICIT_HOST_IN_ALL_LINKS was some global configuration option, but you can ignore this.

    In each controller that required https, I'd add before_filter :require_ssl and add that controller name and its methods to SECURE_ACTIONS. This probably can be improved by passing the action names to the before filter, or something.

    0 讨论(0)
  • If you want all your links to be able to switch between http and https, you have to stop using the _path helper and switch to _url helpers.

    After that, using a scope with the protocol parameter forced and protocol constraint makes the urls automatically switch.

    routes.rb
    scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
      resources :sessions
    end
    
    resources :gizmos
    

    And now in your views:

    <%= sessions_url # => https://..../sessions %>
    <%= gizmos_url   # => http://..../gizmos %>
    

    Edit

    This doesn't fix urls that go back to http when you are in https. To fix that you need to override url_for.

    In any helper
    module ApplicationHelper
      def url_for(options = nil)
        if Hash === options
          options[:protocol] ||= 'http'
        end
        super(options)
      end
    end
    

    This will set the protocol to 'http' unless it was explicitly set (in routes or when calling the helper).

    0 讨论(0)
提交回复
热议问题