How to enable CORS in apache tomcat

Rav*_*ari 20 java eclipse apache tomcat cors

I am trying to consume some web services which are cross domain. When I disable chrome's web-security it is working fine. I want it to work without this so I have tried adding cross-domain.xml and still it didnt work. When i searched more, came to know about CORS enabling in tomcat.

from http://www.w3.org/wiki/CORS_Enabled


For Apache Apache can be configured to expose this header using mod_headers. This is enabled by default in Apache, however you may want to ensure it's enabled in your deployment by running the following command:

a2enmod headers
Run Code Online (Sandbox Code Playgroud)

To expose the header, you can add the following line inside , , and sections, or within an .htaccess file.

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
 </IfModule>
Run Code Online (Sandbox Code Playgroud)

Can anyone please let me know where to add these configurations in TOMCAT and in which files exactly. I am using tomcat from eclipse.

Appreciate any help.

Mar*_*mas 12

Tomcat中的CORS支持通过过滤器提供.您需要将此过滤器添加到web.xml文件并配置它以符合您的要求.有关可用配置选项的完整详细信息,请参阅Tomcat文档.

  • 感谢您的回复.但是我在我的Web.xml中添加了这个,甚至尝试使用servlet拦截器为所有响应添加header参数和response.setHeader("Access-Control-Allow-Origin","*").但它没有用. (13认同)

jos*_*ose 6

检查以下答案:在Tomcat中设置CORS标头

请注意,您需要Tomcat 7.0.41或更高版本。

要知道Tomcat的当前实例在哪里,请尝试以下操作:

System.out.println(System.getProperty("catalina.base"));
Run Code Online (Sandbox Code Playgroud)

您将在控制台视图中看到该路径。

然后在该文件夹中查找/conf/web.xml,将其打开并添加以上链接的各行。