Java 如何在 apache tomcat 中启用 CORS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22526146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:15:35  来源:igfitidea点击:

How to enable CORS in apache tomcat

javaeclipseapachetomcatcors

提问by Ravi Dasari

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.

我正在尝试使用一些跨域的 Web 服务。当我禁用 chrome 的网络安全时,它工作正常。我希望它在没有这个的情况下工作,所以我尝试添加 cross-domain.xml 但它仍然没有工作。当我搜索更多时,才知道在 tomcat 中启用 CORS。

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

来自 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:

对于 Apache Apache 可以配置为使用 mod_headers 公开此标头。这在 Apache 中默认启用,但是您可能希望通过运行以下命令来确保在您的部署中启用它:

a2enmod headers

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

要公开标题,您可以在 、 和 部分或 .htaccess 文件中添加以下行。

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
 </IfModule>


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

任何人都可以让我知道在 TOMCAT 中的何处添加这些配置以及在哪些文件中添加这些配置。我正在使用 eclipse 中的 tomcat。

Appreciate any help.

感谢任何帮助。

采纳答案by Mark Thomas

CORS support in Tomcat is provided via a filter. You need to add this filter to your web.xmlfile and configure it to match your requirements. Full details on the configuration options available can be found in the Tomcat Documentation.

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

回答by jose

Check this answer: Set CORS header in Tomcat

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

Note that you need Tomcat 7.0.41 or higher.

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

To know where is located the current instance of Tomcat try this:

要知道 Tomcat 的当前实例位于何处,请尝试以下操作:

System.out.println(System.getProperty("catalina.base"));

You'll see the path in the console view.

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

Then look for /conf/web.xmlon that folder, open it and add the lines of the above link.

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

回答by Francisco López-Sancho

Just to add a bit of extra info over the right solution. Be aware that you'll need this class org.apache.catalina.filters.CorsFilter. So in order to have it, if your tomcat is not 7.0.41 or higher, download 'tomcat-catalina.7.0.41.jar' or higher ( you can do it from http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina) and put it in the 'lib' folder inside Tomcat installation folders. I actually used 7.0.42 Hope it helps!

只是在正确的解决方案上添加一些额外的信息。请注意,您将需要此类 org.apache.catalina.filters.CorsFilter。因此,为了拥有它,如果您的 tomcat 不是 7.0.41 或更高版本,请下载“tomcat-catalina.7.0.41.jar”或更高版本(您可以从http://mvnrepository.com/artifact/org 下载。 apache.tomcat/tomcat-catalina) 并将其放在 Tomcat 安装文件夹内的“lib”文件夹中。我实际上使用了 7.0.42 希望它有所帮助!