是否有一种方法允许多个跨域使用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中工作。
是否可以指定多个域,还是只能指定一个域?
当前回答
只能为Access-Control-Allow-Origin标头指定一个原点。但是您可以根据请求在响应中设置原点。不要忘记设置Vary标头。在PHP中,我将做以下工作:
/**
* Enable CORS for the passed origins.
* Adds the Access-Control-Allow-Origin header to the response with the origin that matched the one in the request.
* @param array $origins
* @return string|null returns the matched origin or null
*/
function allowOrigins($origins)
{
$val = $_SERVER['HTTP_ORIGIN'] ?? null;
if (in_array($val, $origins, true)) {
header('Access-Control-Allow-Origin: '.$val);
header('Vary: Origin');
return $val;
}
return null;
}
if (allowOrigins(['http://localhost', 'https://localhost'])) {
echo your response here, e.g. token
}
其他回答
谷歌对通过SSL服务广告的支持回答和RFC本身的语法似乎表明您可以用空格分隔url。不知道这在不同的浏览器中有多受支持。
答案似乎是多次使用头文件。也就是说,而不是发送
Access-Control-Allow-Origin: http://domain1.example, http://domain2.example, http://domain3.example
send
Access-Control-Allow-Origin: http://domain1.example
Access-Control-Allow-Origin: http://domain2.example
Access-Control-Allow-Origin: http://domain3.example
在Apache上,你可以在httpd.conf <VirtualHost> section或.htaccess文件中使用mod_headers和以下语法来实现:
Header add Access-Control-Allow-Origin "http://domain1.example"
Header add Access-Control-Allow-Origin "http://domain2.example"
Header add Access-Control-Allow-Origin "http://domain3.example"
诀窍是使用add而不是append作为第一个参数。
如果您像我一样尝试了这么多代码示例以使它使用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标头指定一个原点。但是您可以根据请求在响应中设置原点。不要忘记设置Vary标头。在PHP中,我将做以下工作:
/**
* Enable CORS for the passed origins.
* Adds the Access-Control-Allow-Origin header to the response with the origin that matched the one in the request.
* @param array $origins
* @return string|null returns the matched origin or null
*/
function allowOrigins($origins)
{
$val = $_SERVER['HTTP_ORIGIN'] ?? null;
if (in_array($val, $origins, true)) {
header('Access-Control-Allow-Origin: '.$val);
header('Vary: Origin');
return $val;
}
return null;
}
if (allowOrigins(['http://localhost', 'https://localhost'])) {
echo your response here, e.g. token
}
我也面临着同样的问题。 我的客户端在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();
}