Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS filter tomcat 7 No 'Access-Control-Allow-Origin' header is present on the requested resource

I am using the following configuration in my 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>

My apache webserver sends request to tomcat rest api. APache webserver is running on port 8005 and tomcat is running on 8080. So request goes from mydomain.com:8085/index.php/kop to mydomain.com:8080/webiste-1.0/rest-api/product. I am getting

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

Following is the request header from 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

Response Headers: Content Length: "0" Date: "Wed, 18 Jun 2014 12:02:11 GMT" Server: "Apache-Coyote/1.1"

What am I doing wrong? Am I missing anything?

like image 403
wazzz Avatar asked Jun 18 '14 12:06

wazzz


1 Answers

I found the solution myself. Apache Tomcat includes support for CORS - Starting from Tomcat version 7.0.41 - my problem was I installed tomcat 7 by using apt-get install tomcat7 on ubuntu 12.04 release and it installed older version than 7.0.41. Once I installed tomcat manually 7.0.54, cors filter worked

like image 76
wazzz Avatar answered Nov 09 '22 02:11

wazzz