我正在尝试为所有子域,端口和协议启用CORS。
例如,我希望能够从http://sub.mywebsite.example:8080/到https://www.mywebsite.example/*运行XHR请求
通常情况下,我想启用来自源匹配的请求(并且仅限于):
/ / * .mywebsite.example: * / *
我正在尝试为所有子域,端口和协议启用CORS。
例如,我希望能够从http://sub.mywebsite.example:8080/到https://www.mywebsite.example/*运行XHR请求
通常情况下,我想启用来自源匹配的请求(并且仅限于):
/ / * .mywebsite.example: * / *
当前回答
当从“cookie域”(www.domain.example)读取字体时,我们在静态的“无cookie”域上也遇到了类似的问题,这篇文章是我们的英雄。看这里:我如何修复“丢失跨源资源共享(CORS)响应头”webfont问题?
对于复制/粘贴-r类型(并给一些道具),我从所有的贡献中拼凑起来,并将其添加到站点根目录的。htaccess文件的顶部:
<IfModule mod_headers.c>
<IfModule mod_rewrite.c>
SetEnvIf Origin "http(s)?://(.+\.)?(othersite\.example|mywebsite\.example)(:\d{1,5})?$" CORS=$0
Header set Access-Control-Allow-Origin "%{CORS}e" env=CORS
Header merge Vary "Origin"
</IfModule>
</IfModule>
超级安全,超级优雅。喜欢它:你不必开放你的服务器带宽给资源小偷/热链接者类型。
其他回答
当从“cookie域”(www.domain.example)读取字体时,我们在静态的“无cookie”域上也遇到了类似的问题,这篇文章是我们的英雄。看这里:我如何修复“丢失跨源资源共享(CORS)响应头”webfont问题?
对于复制/粘贴-r类型(并给一些道具),我从所有的贡献中拼凑起来,并将其添加到站点根目录的。htaccess文件的顶部:
<IfModule mod_headers.c>
<IfModule mod_rewrite.c>
SetEnvIf Origin "http(s)?://(.+\.)?(othersite\.example|mywebsite\.example)(:\d{1,5})?$" CORS=$0
Header set Access-Control-Allow-Origin "%{CORS}e" env=CORS
Header merge Vary "Origin"
</IfModule>
</IfModule>
超级安全,超级优雅。喜欢它:你不必开放你的服务器带宽给资源小偷/热链接者类型。
使用@Noyo的解决方案,而不是这个。它更简单、更清晰,而且可能在负载下性能更高。
原来的答案只用于历史目的!!
我在这个问题上做了一些尝试,并提出了这个可重用的.htaccess(或httpd.conf)解决方案,适用于Apache:
<IfModule mod_rewrite.c>
<IfModule mod_headers.c>
# Define the root domain that is allowed
SetEnvIf Origin .+ ACCESS_CONTROL_ROOT=yourdomain.example
# Check that the Origin: matches the defined root domain and capture it in
# an environment var if it does
RewriteEngine On
RewriteCond %{ENV:ACCESS_CONTROL_ROOT} !=""
RewriteCond %{ENV:ACCESS_CONTROL_ORIGIN} =""
RewriteCond %{ENV:ACCESS_CONTROL_ROOT}&%{HTTP:Origin} ^([^&]+)&(https?://(?:.+?\.)?\1(?::\d{1,5})?)$
RewriteRule .* - [E=ACCESS_CONTROL_ORIGIN:%2]
# Set the response header to the captured value if there was a match
Header set Access-Control-Allow-Origin %{ACCESS_CONTROL_ORIGIN}e env=ACCESS_CONTROL_ORIGIN
</IfModule>
</IfModule>
只要将块顶部的ACCESS_CONTROL_ROOT变量设置为您的根域,它将在Access-Control-Allow-Origin: response头值中将Origin: request头值回显给客户端,如果它与您的域匹配。
还请注意,您可以使用sub.mydomain.example作为ACCESS_CONTROL_ROOT,它将限制源为sub.mydomain.example和*.sub.mydomain。示例(即它不必是域根)。可以通过修改正则表达式的URI匹配部分来控制允许变化的元素(协议、端口)。
CORS规范是全有或全无。只支持*、null或精确的协议+域+端口:http://www.w3.org/TR/cors/#access-control-allow-origin-response-header
您的服务器将需要使用正则表达式验证原点标头,然后您可以在Access-Control-Allow-Origin响应标头中回显原点值。
对于Spring Boot,我发现这个RegexCorsConfiguration扩展了官方的CorsConfiguration: https://github.com/looorent/spring-security-jwt/blob/master/src/main/java/be/looorent/security/jwt/RegexCorsConfiguration.java
我不得不稍微修改Lars的回答,因为正则表达式中出现了一个孤立的\,只比较实际的主机(不注意协议或端口),我想在我的生产域之外支持localhost域。因此,我将$allowed参数更改为一个数组。
function getCORSHeaderOrigin($allowed, $input)
{
if ($allowed == '*') {
return '*';
}
if (!is_array($allowed)) {
$allowed = array($allowed);
}
foreach ($allowed as &$value) {
$value = preg_quote($value, '/');
if (($wildcardPos = strpos($value, '\*')) !== false) {
$value = str_replace('\*', '(.*)', $value);
}
}
$regexp = '/^(' . implode('|', $allowed) . ')$/';
$inputHost = parse_url($input, PHP_URL_HOST);
if ($inputHost === null || !preg_match($regexp, $inputHost, $matches)) {
return 'none';
}
return $input;
}
用法如下:
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: " . getCORSHeaderOrigin(array("*.myproduction.com", "localhost"), $_SERVER['HTTP_ORIGIN']));
}