我正在使用 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/