CORS过滤器tomcat 7请求的资源上没有“Access-Control-Allow-Origin”标头

时间:2014-06-18 12:10:49

标签: apache tomcat7 cors

我在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 webserver向tomcat rest api发送请求。 APache webserver在端口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”

我做错了什么?我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

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