是否有一种方法允许多个跨域使用Access-Control-Allow-Origin头?
我知道*,但它太开放了。我只讲几个域。
举个例子,是这样的:
Access-Control-Allow-Origin: http://domain1.example, http://domain2.example
我已经尝试了上面的代码,但它似乎不能在Firefox中工作。
是否可以指定多个域,还是只能指定一个域?
是否有一种方法允许多个跨域使用Access-Control-Allow-Origin头?
我知道*,但它太开放了。我只讲几个域。
举个例子,是这样的:
Access-Control-Allow-Origin: http://domain1.example, http://domain2.example
我已经尝试了上面的代码,但它似乎不能在Firefox中工作。
是否可以指定多个域,还是只能指定一个域?
当前回答
为了对. net应用程序进行相当简单的复制/粘贴,我写这个代码是从全局变量中启用CORS。asax文件。这段代码遵循当前接受的回答中给出的建议,将请求中给出的任何原点反映到响应中。这有效地实现了'*'而不使用它。
这样做的原因是它支持多种其他CORS功能,包括发送带有'withCredentials'属性设置为'true'的AJAX XMLHttpRequest的能力。
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.HttpMethod == "OPTIONS")
{
Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
Response.AddHeader("Access-Control-Max-Age", "1728000");
Response.End();
}
else
{
Response.AddHeader("Access-Control-Allow-Credentials", "true");
if (Request.Headers["Origin"] != null)
Response.AddHeader("Access-Control-Allow-Origin" , Request.Headers["Origin"]);
else
Response.AddHeader("Access-Control-Allow-Origin" , "*");
}
}
其他回答
为了方便ASMX服务的多域访问,我在全局中创建了这个函数。asax文件:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string CORSServices = "/account.asmx|/account2.asmx";
if (CORSServices.IndexOf(HttpContext.Current.Request.Url.AbsolutePath) > -1)
{
string allowedDomains = "http://xxx.yyy.example|http://aaa.bbb.example";
if(allowedDomains.IndexOf(HttpContext.Current.Request.Headers["Origin"]) > -1)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", HttpContext.Current.Request.Headers["Origin"]);
if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
HttpContext.Current.Response.End();
}
}
这也允许CORS处理OPTIONS动词。
我也面临着同样的问题。 我的客户端在9097,api网关在9098,微服务在.... 实际上我用的是spring cloud Api网关 在我的网关yml文件中,我允许像——这样的交叉源 ... allowedOrigins:“http://localhost: 9097”
在我的微服务中,我也使用了@crossOrigin
当客户端向api网关发送请求时,两个“Access-Control-Allow-Origin”报头正在响应[一个来自api yml文件,一个来自microservice @crossorigin] 浏览器阻止请求
我把它解成
@Bean
public RouteLocator getRL(RouteLocatorBuilder builder) {
return builder.routes()
.route(p->
"/friendlist","/guest/**"
)
.filters(f ->{
//f.removeResponseHeader("Access-Control-Allow-Origin");
//f.addResponseHeader("Access-Control-Allow-Origin","http://localhost:9097");
f.setResponseHeader("Access-Control-Allow-Origin","http://localhost:9097");
return f;
})
.uri("lb://OLD-SERVICE")
).build();
}
如果您像我一样尝试了这么多代码示例以使它使用CORS工作,值得一提的是,您必须首先清除缓存,以尝试它是否实际工作,类似于旧图像仍然存在的问题,即使它在服务器上被删除了(因为它仍然保存在缓存中)。
例如,CTRL + SHIFT + DEL在谷歌Chrome删除你的缓存。
这帮助我在尝试了许多纯。htaccess解决方案后使用这段代码,这似乎是唯一一个工作(至少对我来说):
Header add Access-Control-Allow-Origin "http://google.com"
Header add Access-Control-Allow-Headers "authorization, origin, user-token, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
<FilesMatch "\.(ttf|otf|eot|woff)$">
<IfModule mod_headers.c>
SetEnvIf Origin "http(s)?://(www\.)?(google.com|staging.google.com|development.google.com|otherdomain.com|dev02.otherdomain.net)$" AccessControlAllowOrigin=$0
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
</FilesMatch>
还要注意,很多解决方案都说你必须输入Header set…但它是头添加....希望这能帮助到像我这样有同样问题的人。
如上所述,Access-Control-Allow-Origin应该是唯一的,如果您位于CDN(内容分发网络)后面,则应该将Vary设置为Origin。
Nginx配置的相关部分:
if ($http_origin ~* (https?://.*\.mydomain\.com(:[0-9]+)?)) {
set $cors "true";
}
if ($http_origin ~* (https?://.*\.my-other-domain\.com(:[0-9]+)?)) {
set $cors "true";
}
if ($cors = "true") {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'X-Frame-Options' "ALLOW FROM $http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Vary' 'Origin';
}
对于安装了URL重写2.0模块的iis7.5 +,请参阅此SO答案