繁体   English   中英

Rails 3 SSL路由从https重定向到http

[英]Rails 3 SSL routing redirects from https to http

这个问题涉及到这个问题和答案(rails-3-ssl-deprecation) ,其中建议使用routes.rb和路由来处理rails 3中的ssl:

resources :sessions, :constraints => { :protocol => "https" }

# Redirect /foos and anything starting with /foos/ to https.
match "foos(/*path)", :to => redirect { |_, request|  "https://" + request.host_with_port + request.fullpath }

我的问题是链接使用相对路径(我认为这是正确的术语),一旦我在https页面上,所有其他链接到网站上的其他页面然后使用https。

1)对于不需要https的页面,回到http的最佳方法是什么? 我是否必须为所有这些设置重定向(我希望注意)或者有更好的方法。 重定向会是这样的:

match "foos(/*path)", :to => redirect { |_, request|  "http://" + request.host_with_port + request.fullpath }

2)如果需要重定向回http,我该如何处理我希望所有方法都是http的情况除外? 即foos(/ * path)适用于所有foos方法。 但是说我想要foos / upload_foos来使用ssl。 我知道如何要求它

scope :constraints => { :protocol => "https" } do
  match 'upload_foos' => 'foos#upload_foos', :via => :post, :as => :upload_foos 
end

但是,如果我将http重定向放入foos路径,那么https upload_foos会发生什么?

如果您希望所有链接都能够在http和https之间切换,则必须停止使用_path帮助程序并切换到_url帮助程序。

之后,使用带有协议参数强制和协议约束的作用域使URL自动切换。

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

现在在你的观点中:

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

编辑

当您使用https时,这不会修复返回http的网址。 要解决此问题,您需要覆盖url_for

在任何帮手
 module ApplicationHelper def url_for(options = nil) if Hash === options options[:protocol] ||= 'http' end super(options) end end 

除非已明确设置(在路由中或调用帮助程序时),否则这会将协议设置为“http”。

这是很久以前的事了,我确信它可以改进,但回到一些旧版本的rails我在应用程序控制器中有这个代码。 不确定这对Rails 3是否仍然有效,但它可能会有所帮助:

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

USE_EXPLICIT_HOST_IN_ALL_LINKS是一些全局配置选项,但您可以忽略它。

在每个需要https的控制器中,我都会添加before_filter :require_ssl并将该控制器名称及其方法添加到SECURE_ACTIONS 这可能通过将动作名称传递给之前的过滤器或其他东西来改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM