Access-Control-Allow-Origin:*在tomcat中 [英] Access-Control-Allow-Origin: * in tomcat
问题描述
我想在我的tomcat容器中设置默认的http标头 -
I want to set a default http header in my tomcat container -
Access-Control-Allow-Origin:*
Access-Control-Allow-Origin: *
来自stackoverslow和谷歌的各种不同链接,大多数都指向资源。 这个再次说明如何做到这一点。我已经复制了相同的内容,但仍未显示标题。
这就是我所做的:
1)在tomcat的/ lib文件夹中复制cors.jar文件
2)在web.xml中插入此文本
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>
它不起作用,我很无奈。我的tomcat版本是-6.0.6。请帮助。
It doesn't work and I am helpless. My tomcat version is -6.0.6. Please help.
推荐答案
问题出现是因为不包括 jar
文件作为项目的一部分。我只是把它包含在tomcat lib中。使用 web.xml
中的以下内容现在可以使用:
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>
以下项目依赖项:
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>1.3.2</version>
</dependency>
这篇关于Access-Control-Allow-Origin:*在tomcat中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!