【问题标题】:Heroku Rails Force SSL Redirect LoopHeroku Rails 强制 SSL 重定向循环
【发布时间】:2014-09-18 11:06:32
【问题描述】:

所以我有一个 Rails 应用程序,它在 Heroku 上有一个暂存环境和一个生产环境。我想在生产环境中强制使用 ssl,而不是在暂存环境中强制使用 ssl。我的应用程序控制器(如下)显示了我的配置方式,但我的暂存环境仍在尝试强制 ssl 工作,这导致了重定向循环。

我的问题是:A) 我如何停止重定向循环并让应用程序不会因强制使用 ssl 而崩溃?和 B) 我必须做什么才能阻止 force_ssl 在我尚未完成的暂存环境中发生?

谢谢!

class ApplicationController < ActionController::Base
  force_ssl if: :ssl_configured?
  before_action :configure_permitted_parameters, if: :devise_controller?

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception


  # Make application helpers availble within controllers
  include ApplicationHelper

 # enable_authorization :unless => :devise_controller? # ACl

  before_filter do # ACL work around for attribute mass assignment
    resource = controller_path.singularize.gsub('/', '_').to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end

  rescue_from CanCan::Unauthorized do |exception|
    redirect_to main_app.root_path, :alert => exception.message
  end

  #handling redirection after login basing on the just logged in user role
  def after_sign_in_path_for(user)
    if user.has_role?(:director)
      unless user.organization.nil?
        dashboard_organization_path(user.organization.id) 
      else
        organization_trainings_url
      end
    elsif user.has_role?(:user)
      user_path(user)
    elsif user.has_role?(:admin)
      organization_trainings_url
    else
      root_url
    end
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name, :title, :phone, :email,
                                                 :organization_id, :badge, :dl,
                                                 :hire_date, :dl_expiration, :leader_id, :password, :current_password,
                                                 :division_id, :bureau_id]
  end 

  private 

  def ssl_configured?
    Rails.env.production?
  end

end

【问题讨论】:

  • 你检查过staging是否真的是staging吗? Rails.env.production? 在您的 heroku 控制台中返回什么?
  • false 是我输入时得到的结果
  • 还有Rails.env.staging??
  • 你解决了吗?我也有同样的问题。
  • 这里也一样。有什么见解吗?

标签: ruby-on-rails ruby ssl heroku ruby-on-rails-4


【解决方案1】:

您可以为此利用Heroku Config Variables

例如,您可以使用

设置一个名为 APP_NAME 的配置变量

heroku config:set APP_NAME=my-app-name

在您的每个 Heroku 服务器上,以便您可以在代码中区分它们。您可以按如下方式使用此环境/配置变量:

# in production.rb
config.force_ssl = true unless ENV['APP_NAME'] == 'my-staging-app-name'

恕我直言,更好的方法是为您尝试执行的操作设置一个变量/标志,例如 SKIP_FORCE_SSL. 所以,首先,您可以在 Heroku 上设置配置变量:

heroku config:set SKIP_FORCE_SSL=true

然后,在您的应用程序中,您可以检查是否应该强制使用 SSL:

config.force_ssl = true unless ENV['SKIP_FORCE_SSL']

我建议使用上述黑名单方法,以便在应用找不到环境变量或您要检查的任何其他内容时默认强制 SSL。

如果您需要在生产模式下运行时在开发机器上进行测试(将使用production.rb),您可以在~/.bash_profile 中设置相同的环境变量:

export SKIP_FORCE_SSL='true'

(请记住,您需要打开一个新的控制台选项卡/窗口才能使其生效。另请注意,环境变量将以字符串形式返回,而不是布尔值,即使您省略了这些引号。)

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 2011-11-03
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多