[英]ruby on rails - rack-cors multiple origins with different resources
我正在使用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'
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
经过检查和测试后发现它是正确的语法。 您可以根据需要添加任意数量的块:
allow do
origins '[the domain]'
resource '[the resource/directories]', headers: :any, methods: [:get, :post, :options, :put, :delete]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.