ruby-on-rails - ruby on rails - rack-cors 具有不同资源的多个来源

标签 ruby-on-rails ruby rack-cors

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

我需要这样的东西:

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 ' 访问所有。可能吗?

上面的代码是正确的代码/语法吗?

谢谢。

最佳答案

我知道这有点老了,但对于那些发现这个问题的人,我只使用 Rails 5.1.4 api 以不同的方式解决这个问题

-

起源

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

科尔斯

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

关于ruby-on-rails - ruby on rails - rack-cors 具有不同资源的多个来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41589352/

相关文章:

ruby - 为什么向模块添加方法与添加子模块的处理方式不同?

ruby-on-rails - Rails 5.1 CORS - 如何为不同环境设置不同来源

ruby-on-rails - Rails - 仅在开发中允许在 CORS 设置中使用 localhost

ruby-on-rails - Rails has_one :through association

ruby-on-rails - shell/ ruby /RubyMine : Accessing underlying shell environment variables

ruby-on-rails - delayed_job 处理作业后的回调

ruby-on-rails - Rspec测试加载错误: cannot load such file -- spec_helper (LoadError)

ruby - RVM安装过程中出现 "gpg: command not found"错误如何解决?

ruby-on-rails - Rails 6、React、Rack-Cors

ruby - 解码 Auth0 token 时遇到问题