【问题标题】:Using CORS with Rails 4 and heroku在 Rails 4 和 heroku 中使用 CORS
【发布时间】:2014-06-20 19:47:29
【问题描述】:

我在 heroku 上使用 Rails 4,需要启用 CORS。

我已经在网上浏览了几个小时,并尝试了各种解决方案,这些解决方案对许多其他人都有效,但不适用于我。

我最后一次尝试是简单地将 CORS 添加到应用程序控制器中的所有请求中:

before_filter :cors_preflight_check
after_filter :set_headers

def set_headers
    #if request.headers["HTTP_ORIGIN"]
      # better way check origin
      #if request.headers["HTTP_ORIGIN"] && /^https?:\/\/(.*)\.(.*)\.cloudfront\.net$/i.match(request.headers["HTTP_ORIGIN"])
        headers['Access-Control-Allow-Origin'] = '*'
        headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
        headers['Access-Control-Allow-Headers'] = %w{Origin Accept Content-Type X-Requested-With auth_token X-CSRF-Token}.join(',')
        headers['Access-Control-Max-Age'] = "1728000"
      #end
    #end
  end

  def cors_preflight_check
    if request.method == "OPTIONS"
      headers['Access-Control-Allow-Origin'] = 'http://localhost'
      headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
      headers['Access-Control-Allow-Headers'] = %w{Origin Accept Content-Type X-Requested-With auth_token X-CSRF-Token}.join(',')
      headers['Access-Control-Max-Age'] = '1728000'
      render :text => '', :content_type => 'text/plain'
    end
  end

注释行是为了使所有内容更加干净。但是,这些都不起作用。这就是this thread 告诉我的做法。

你有什么想法或提示我如何让它工作吗?

提前致谢!

【问题讨论】:

  • 这是一个很好的问题,需要回答,我自己已经找了几个小时了。
  • 我现在就发布我的解决方案
  • 我昨晚遇到了这个问题,我很不高兴!

标签: heroku ruby-on-rails-4 cors assets


【解决方案1】:

所以你几乎拥有我所拥有的一切,但我认为你没有将选项请求与你的 cors_preflight_check 方法相匹配。您还可以删除 cors_preflight 请求上的 before 过滤器,因为默认情况下选项将被路由到那里。如果您对请求有任何身份验证,则需要为选项请求删除它。看看这个https://github.com/cleor41/Cors-Rails4-API,我会在这里发布重要的部分。

这会在您的 API 的应用程序控制器中进行

#APIController

before_action :authenticate_user
after_filter :cors_set_access_control_headers
skip_before_filter :authenticate_user, :only => [:route_options]

def route_options
  cors_preflight_check
end

private

  def authenticate_user
    #Do some cool stuff with tokens to identify the user
  end

  def cors_set_access_control_headers
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email'
    response.headers['Access-Control-Max-Age'] = "1728000"
  end

  def cors_preflight_check
    if request.method == 'OPTIONS'
      request.headers['Access-Control-Allow-Origin'] = '*'
      request.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
      request.headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token, Auth-Token, Email'
      request.headers['Access-Control-Max-Age'] = '1728000'  
      render :text => '', :content_type => 'text/plain'
    end
  end

这会进入您的路线并与您的 api 应用程序控制器中的 cors_preflight_check 匹配

#Routes

#Route this to wherever you put the cors_preflight_check
#This is to handle the CORS preflight request, it only catches the options action.
controller 'api/v1/api' do
  match '*unmatched_route', :to => 'api/v1/api#route_options', via: [:options]
end

希望这对你有用,如果没有,请告诉我。

【讨论】:

  • 这对我有用,但我决定使用 rails cors gem,因为它不会污染我的应用程序控制器,我讨厌看这个。如果你走这条路,我建议创建一个 cors 对象或其他东西并将其填充在那里。
猜你喜欢
  • 2013-11-25
  • 2015-09-11
  • 2014-05-05
  • 2013-11-21
  • 2013-11-10
  • 2013-06-20
  • 1970-01-01
  • 2023-03-31
  • 2014-08-09
相关资源
最近更新 更多