yl2*_*015 7 ruby ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)
所以它允许" http:// localhost:3000 "只访问'/ api/*'并允许' http:// localhost:6000 '访问所有.可能吗?
上面的代码是正确的代码/语法吗?
谢谢.
小智 12
我知道这有点旧了但是对于那些发现这个的人我只用Rails 5.1.4 api来解决这个问题
-
起源
ENV['CORS_ORIGINS'] = 'https://domain.first.com, http://another.origin.io'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
经过检查和测试,结果证明它是正确的语法。您可以根据需要添加任意数量的块:
allow do
origins '[the domain]'
resource '[the resource/directories]', headers: :any, methods: [:get, :post, :options, :put, :delete]
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |