如何只为选定的路由启用 CORS rails
How to enable CORS for only selected route rails
我在使用 Rails5,我想在我的其中一条路线上允许 CORS。这是我如何允许我的所有路由使用 CORS,但是有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
要仅允许对特定端点路径的跨源请求,请将其用作第一个 resource
参数:
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
我在使用 Rails5,我想在我的其中一条路线上允许 CORS。这是我如何允许我的所有路由使用 CORS,但是有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
要仅允许对特定端点路径的跨源请求,请将其用作第一个 resource
参数:
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