[英]ruby on rails - rack-cors multiple origins with different resources
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. 我正在使用rack-cors gem在我的rails应用程序中实现CORS,但我不确定如何为不同的来源定义不同的资源。
I need something like that: 我需要这样的东西:
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
So it will allow " http://localhost:3000 " to access only '/api/*' and allow ' http://localhost:6000 ' to access all. 所以它允许“ http:// localhost:3000 ”只访问'/ api / *'并允许' http:// localhost:6000 '访问所有。 is it possible?
可能吗?
is the above code the correct code/syntax for doing that? 上面的代码是正确的代码/语法吗?
thanks. 谢谢。
I know this is a little old but for those finding this I am solving this differently with Rails 5.1.4 api only 我知道这有点旧了但是对于那些发现这个的人我只用Rails 5.1.4 api来解决这个问题
- -
Origins 起源
ENV['CORS_ORIGINS'] = 'https://domain.first.com, http://another.origin.io'
Cors 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
After checking and testing it turns out it is the right syntax. 经过检查和测试后发现它是正确的语法。 You can add as many blocks as you need:
您可以根据需要添加任意数量的块:
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.