Heroku,Rails:WildCard SSL导致env ['warden']返回nil

时间:2012-11-21 09:47:12

标签: ruby-on-rails ssl heroku devise warden

在我的rails应用中。我正在使用设计。用户将从几个子域名进入首页,例如“ja.myapp.com”,“www.myapp.com”等。 我只在促销页面内使用多个子域名,因此当他们进入“登录”页面时,所有这些子域名都会重定向到“www”子域名页面。

当用户登录时,如果用户的profile_type为“Student”,则用户将重定向到“Students'home”。如果是“Teacher”,“Teachers'home”。

当我使用“POW”进行测试时,一切正常,但是当我将其上传到生产服务器时。 用户登录后,所有用户都将重定向到TOP页面。

我回滚了应用程序,并尝试再次登录,但同样的事情发生了 这是在我将heroku证书从“单域SSL”上传到“通配符子域SSL”之后发生的。

将SSL更改为单域域后,在回滚站点内不再发生这种情况。

我认为这是因为“通配符子域SSL”和&设计会话控制器,但不太确定。

有没有人有任何想法解决这个问题?

以下是代码。

的routes.rb

root :to => 'students#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Student' }
root :to => 'teachers#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Teacher' }
root :to => 'pages#home'
devise_for :users, :controllers => {:sessions => "sessions", :registrations => "registrations", :confirmations => "confirmations"}

配置/初始化/ session_store.rb

MyAPP::Application.config.session_store :cookie_store, key: '_my_app_secure_session', domain: :all

# coding: utf-8
class SessionsController < Devise::SessionsController
  after_filter :clear_sign_signout_flash
  def after_sign_in_path_for(resource)
    url = root_path
  end

  def new
    @title = I18n.t "sessions.new.title"
    super
  end

  def create
    super
  end

  def destroy
    super
  end

  protected
  def clear_sign_signout_flash
    if flash.keys.include?(:notice)
      flash.delete(:notice)
    end
  end

end

application_controller

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_locale, :www_redirect

  private
  def www_redirect
    #if Rails.env.production
      parsed_subdomain = request.subdomains.first
      locale = Locale.find_by_subdomain(parsed_subdomain)
      if (use_www? && parsed_subdomain != "www") || locale.nil?
        redirect_to request.url.sub(parsed_subdomain, "www")
      end
    #end
  end

  def use_www?
    true
  end

  def set_locale
    if user_signed_in? 
      if current_user.profile_type == "Teacher"
        I18n.locale = I18n.default_locale
      else
        I18n.locale = current_user.locale.i18n_form.to_sym
      end
    else
      if request.subdomains.first == "www" && cookies[:locale]
        I18n.locale = cookies[:locale].to_sym
      else
        I18n.locale = extract_locale_from_subdomain || I18n.default_locale
        cookies[:locale] = {
          value: I18n.locale.to_s,
          expires: 1.year.from_now,
          domain: :all
        }
      end
    end
  end

  def extract_locale_from_subdomain
    parsed_subdomain = request.subdomains.first
    locale = Locale.find_by_subdomain(parsed_subdomain)
    if locale
      I18n.available_locales.include?(locale.i18n_form.to_sym) ? locale.i18n_form.to_sym : nil
    else
      nil
    end
  end
end

locale_settings_controller.rb

class LocaleSettingsController < ApplicationController

  def switch_language
    locale = params[:locale]
    locale = "www" if locale == "en"
    path = params[:uri]
    cookies[:locale] = {
      value: locale,
      expires: 1.year.from_now,
      domain: :all
    }
    if Rails.env.production?
      base_url = "http://" + "#{locale}" + ".myapp.com"
    else
      base_url = "http://" + "#{locale}" + ".myapp.dev"
    end
    url = path == "/" ?  base_url : base_url + "#{path}"
    redirect_to url
  end
end

production.rb

config.force_ssl = true

配置/初始化/ rack_rewrite.rb

require 'rack/rewrite'

LingualBox::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{.*}, 'http://www.myapp.com$&', :if => Proc.new {|rack_env|
    rack_env['SERVER_NAME'] == 'myapp.com'
  }
end if Rails.env == 'production'

0 个答案:

没有答案