我在Rails5上,我想在我的一条路线上允许CORS。以下是我可以为所有路由提供CORS的方法,但有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
答案 0 :(得分:6)
要仅允许特定端点路径的跨源请求,请将其用作第一个resource
arg:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/endpoint/to/allow', :headers => :any, :methods => [:get, :post, :options]
end
end
这将仅允许路径/endpoint/to/allow
的跨源请求。
如果要允许多个路径,可以指定多个resource
声明:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/endpoint/to/allow', :headers => :any, :methods => [:get, :post, :options]
resource '/another/endpoint/', :headers => :any, :methods => [:get, :post, :options]
end
end