Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access-Control-Allow-Origin: * in tomcat

I want to set a default http header in my tomcat container -

Access-Control-Allow-Origin: *

From various different links on stackoverslow and from google, most have pointed to a resource. This again says same on how to do it. I have replicated the same, but still the header is not shown. This is what I have done: 1) Copied cors.jar file in /lib folder of tomcat 2) Inserted this text in web.xml

<filter>     <filter-name>CORS</filter-name>     <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>     <init-param>      <param-name>cors.allowOrigin</param-name>         <param-value>*</param-value>     </init-param>     <init-param>      <param-name>cors.supportedMethods</param-name>         <param-value>GET, POST, HEAD, PUT, DELETE</param-value>     </init-param>     <init-param>      <param-name>cors.supportedHeaders</param-name>         <param-value>Content-Type, Last-Modified</param-value>     </init-param>     <init-param>         <param-name>cors.exposedHeaders</param-name>         <param-value>Set-Cookie</param-value>     </init-param>     <init-param>         <param-name>cors.supportsCredentials</param-name>         <param-value>true</param-value>     </init-param> </filter>  <filter-mapping>     <filter-name>CORS</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping> 

It doesn't work and I am helpless. My tomcat version is -6.0.6. Please help.

like image 948
Jatin Avatar asked Sep 12 '12 07:09

Jatin


People also ask

How do I enable CORS in Tomcat 9?

Tomcat Web Server Config To enable CORS support we have to use CORS Filter. If you want to enable CORS for all webapps, add the filter into $CATALINA_BASE/conf/web. xml. If you want to enable them only for the MOTECH application, add the filter into $CATALINA_BASE/webapps/motech-platform-server/WEB-INF/web.

How do I give Access-Control allow origin?

Limiting the possible Access-Control-Allow-Origin values to a set of allowed origins requires code on the server side to check the value of the Origin request header, compare that to a list of allowed origins, and then if the Origin value is in the list, set the Access-Control-Allow-Origin value to the same value as ...

Is Access-Control allow Origin * Safe?

Access-Control-Allow-Origin: * is totally safe to add to any resource, unless that resource contains private data protected by something other than standard credentials. Standard credentials are cookies, HTTP basic auth, and TLS client certificates.

How do I fix Access-Control allow Origin error?

Run the following command to confirm the origin server returns the Access-Control-Allow-Origin header. Replace example.com with the required origin header. Replace https://www.example.net/video/call/System.generateId.dwr with the URL of the resource that's returning the header error.


2 Answers

At the time of writing this, the current version of Tomcat 7 (7.0.41) has a built-in CORS filter http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter

like image 143
Pere Barceló Avatar answered Sep 22 '22 23:09

Pere Barceló


The issue arose because of not including jar file as part of the project. I was just including it in tomcat lib. Using the below in web.xml works now:

<filter>     <filter-name>springSecurityFilterChain</filter-name>     <filter-class>         org.springframework.web.filter.DelegatingFilterProxy     </filter-class> </filter>   <filter-mapping>     <filter-name>springSecurityFilterChain</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping>  <filter>     <filter-name>CORS</filter-name>     <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>      <init-param>         <param-name>cors.allowOrigin</param-name>         <param-value>*</param-value>     </init-param>     <init-param>         <param-name>cors.supportsCredentials</param-name>         <param-value>false</param-value>     </init-param>     <init-param>         <param-name>cors.supportedHeaders</param-name>         <param-value>accept, authorization, origin</param-value>     </init-param>     <init-param>         <param-name>cors.supportedMethods</param-name>         <param-value>GET, POST, HEAD, OPTIONS</param-value>     </init-param> </filter>   <filter-mapping>     <filter-name>CORS</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping> 

And the below in your project dependency:

<dependency>     <groupId>com.thetransactioncompany</groupId>     <artifactId>cors-filter</artifactId>     <version>1.3.2</version> </dependency> 
like image 28
Jatin Avatar answered Sep 20 '22 23:09

Jatin