轨道上的红宝石-带有不同资源的多个来源的货架 [英] ruby on rails - rack-cors multiple origins with different resources

查看:94
本文介绍了轨道上的红宝石-带有不同资源的多个来源的货架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rack-cors gem在我的rails应用程序中实现CORS,但是我不确定如何为不同的来源定义不同的资源.

I'm implementing CORS in my rails application using rack-cors gem for it, but I'm not sure how can i define different resources for different origins.

我需要这样的东西:

config.middleware.insert_before 0, Rack::Cors do

  allow do
    origins 'http://localhost:3000'
    resource '/api/*', headers: :any, methods: [:get, :post, :options, :put, :delete]
  end

  allow do
    origins 'http://localhost:6000'
    resource '*', headers: :any, methods: [:get, :post, :options, :put, :delete]
  end

end

因此它将允许" http://localhost:3000 仅访问'/api/*' 并允许" http://localhost:6000 "访问全部.有可能吗?

So it will allow "http://localhost:3000" to access only '/api/*' and allow 'http://localhost:6000' to access all. is it possible?

以上代码是执行此操作的正确代码/语法吗?

is the above code the correct code/syntax for doing that?

谢谢.

推荐答案

我知道这有点旧,但是对于那些发现此问题的人,我仅使用Rails 5.1.4 api来解决此问题

I know this is a little old but for those finding this I am solving this differently with Rails 5.1.4 api only

-

来源

ENV['CORS_ORIGINS'] = 'https://domain.first.com, http://another.origin.io'

Cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins ENV['CORS_ORIGINS'].split(',').map { |origin| origin.strip }

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

这篇关于轨道上的红宝石-带有不同资源的多个来源的货架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆