CORS过滤器Tomcat 7:请求的资源上不存在“Access-Control-Allow-Origin”头信息。

7
我正在使用以下配置在我的tomcat7 conf/web.xml文件中。
<filter>
 <filter-name>CorsFilter</filter-name>
 <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
 <init-param>
  <param-name>cors.allowed.origins</param-name>
  <param-value>*</param-value>
 </init-param>
 <init-param>
  <param-name>cors.allowed.methods</param-name>
  <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
 </init-param>
 <init-param>
  <param-name>cors.allowed.headers</param-name>
  <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Last-Modified</param-value>
 </init-param>
 <init-param>
  <param-name>cors.exposed.headers</param-name>
  <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
 </init-param>
 <init-param>
  <param-name>cors.support.credentials</param-name>
  <param-value>true</param-value>
 </init-param>

</filter>
<filter-mapping>
 <filter-name>CorsFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

我的Apache Web服务器向Tomcat REST API发送请求。Apache Web服务器在端口8005上运行,而Tomcat在8080上运行。因此请求从mydomain.com:8085/index.php/kop发送到mydomain.com:8080/webiste-1.0/rest-api/product。我收到了:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://x.x.x.x:8085' is    therefore not allowed access.

以下是来自 Chrome 的请求头部:

Accept:application/json, text/javascript, */*; q=0.01
Cache-Control:no-cache
Origin:http://x.x.x.x:8085
Pragma:no-cache
Referer:http://x.x.x.x:8085/html/index.php/kop_och_salj
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153    Safari/537.36

响应头信息: 内容长度: "0" 日期: "2014年6月18日星期三12:02:11 GMT" 服务器: "Apache-Coyote/1.1"

我做错了什么?有什么我漏掉的吗?


我已经在我的Apache Web服务器上添加了Header set Access-Control-Allow-Origin "",curl显示Header Access-Control-Allow-Origin "",但仍然出现相同的错误。 - wazzz
我有完全相同的问题。 - naoru
1
Apache Tomcat 包括对 CORS 的支持 - 从 Tomcat 版本 7.0.41 开始 - 我的问题是我在 Ubuntu 12.04 发行版上使用 apt-get install tomcat7 安装了 tomcat 7,但它安装的版本比 7.0.41 更旧。一旦我手动安装了 tomcat 7.0.54,CORS 过滤器就可以正常工作了。 - wazzz
@wazzz 这非常有趣,我建议你像这里解释的那样回复自己:http://stackoverflow.com/help/self-answer - Samuel EUSTACHI
这个回答解决了你的问题吗?在Tomcat中使用Access-Control-Allow-Origin: * - Josh Correia
1个回答

3

我自己找到了解决方案。Apache Tomcat包含CORS支持 - 从Tomcat版本7.0.41开始 - 我的问题是我使用apt-get install tomcat7在ubuntu 12.04版本上安装了tomcat 7,它安装了比7.0.41更旧的版本。一旦我手动安装了tomcat 7.0.54,cors过滤器就起作用了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接