所以我有一个Rails应用程序,它在Heroku上有一个暂存环境以及一个生产环境。我想在生产环境中强制执行ssl,但不要在staging上强制执行。我的应用程序控制器(如下所示)显示了我是如何配置的,但我的暂存环境仍然试图强制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
答案 0 :(得分: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'
(请记住,您需要打开一个新的控制台选项卡/窗口才能使其生效。另请注意,即使您不使用这些引号,环境变量也会以字符串形式返回,而不是布尔值。)
希望这有帮助!