Tomcat 5.5的跨源资源共享 [英] Cross-origin resource sharing for Tomcat 5.5
问题描述
我是跨源资源共享的新手,我想在Tomcat 5.5服务器中启用它。
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?
我想为所有请求设置头文件,并允许所有的请求( Access-Control-Allow-Origin:*
)
I want to set the header universally for all requests, and to allow all origins (Access-Control-Allow-Origin: *
)
推荐答案
如果是静态网站,使用Tomcat 7.0.41,您可以通过构建来轻松控制CORS行为在过滤器中。
If it's a static site, then starting with Tomcat 7.0.41, you can easily control CORS behavior via a built-in filter.
很多事情你只需要编辑全局 web.xml
CATALINA_HOME / conf
并添加过滤器定义:
Pretty much the only thing you have to do is edit the global web.xml
in CATALINA_HOME/conf
and add the filter definition:
<!-- ================== 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 ====================== -->
请注意,Firefox不喜欢 Access-Control-Allow-Origin: *
和具有凭证(Cookie)的请求: 响应具有凭据的请求时,服务器必须指定一个域,并且不能使用通配符。
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.
这篇关于Tomcat 5.5的跨源资源共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!