javascript Tomcat 5.5 跨域资源共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9614605/
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-10-26 07:13:33  来源:igfitidea点击:

Cross-origin resource sharing for Tomcat 5.5

javascripttomcathttp-headerscross-domaincors

提问by Pablo

I am new to Cross-origin resource sharing and I want to enable it in a Tomcat 5.5 server. Anybody can give me some hint how can this be achieved?

我是跨域资源共享的新手,我想在 Tomcat 5.5 服务器中启用它。任何人都可以给我一些提示,这是如何实现的?

I want to set the header universally for all requests, and to allow all origins (Access-Control-Allow-Origin: *)

我想为所有请求普遍设置标头,并允许所有来源 ( Access-Control-Allow-Origin: *)

回答by Johannes Jander

If it's a static site, then starting with Tomcat 7.0.41, you can easily control CORS behavior via a built-in filter.

如果是静态站点,那么从 Tomcat 7.0.41 开始,您可以通过内置过滤器轻松控制 CORS 行为。

Pretty much the only thing you have to do is edit the global web.xmlin CATALINA_HOME/confand add the filter definition:

您几乎唯一要做的就是编辑全局web.xml输入CATALINA_HOME/conf并添加过滤器定义:

     <!-- ================== Built In Filter Definitions ===================== -->

      ...

     <filter>
       <filter-name>CorsFilter</filter-name>
       <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
     </filter>
     <filter-mapping>
       <filter-name>CorsFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>

    <!-- ==================== Built In Filter Mappings ====================== -->

Be aware, though, that Firefox does not like Access-Control-Allow-Origin: *and requests with credentials (cookies): when responding to a credentialed request, server must specify a domain, and cannot use wild carding.

但是请注意,Firefox 不喜欢Access-Control-Allow-Origin: *使用凭据 (cookie) 请求:响应凭据请求时,服务器必须指定域,并且不能使用通配符。

回答by monsur

Here is a Tomcat filter for adding CORS support: https://bitbucket.org/jsumners/corsfilter

这是用于添加 CORS 支持的 Tomcat 过滤器:https: //bitbucket.org/jsumners/corsfilter