ruby-on-rails – 如何仅为选定的路由轨启用CORS
发布时间:2020-12-17 03:07:45 所属栏目:百科 来源:网络整理
导读:我在Rails5上,我想在我的一条路线上允许CORS.以下是我可以在所有路由中使用CORS的方法,但有没有办法只为一个端点列入白名单? config.middleware.insert_before 0,Rack::Cors do allow do origins '*' resource '*',:headers = :any,:methods = [:get,:post,
我在Rails5上,我想在我的一条路线上允许CORS.以下是我可以在所有路由中使用CORS的方法,但有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0,Rack::Cors do allow do origins '*' resource '*',:headers => :any,:methods => [:get,:post,:options] end end 解决方法
要仅允许特定端点路径的跨源请求,请将其用作第一个资源arg:
config.middleware.insert_before 0,Rack::Cors do allow do origins '*' resource '/endpoint/to/allow',:options] end end 这将允许仅针对路径/端点/到/允许的跨源请求. 如果要允许多个路径,可以指定多个资源声明: config.middleware.insert_before 0,:options] resource '/another/endpoint/',:options] end end https://github.com/cyu/rack-cors#resource有更多细节. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |