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 -
访问控制允许来源:*
从 stackoverslow 和 google 上的各种不同链接,大多数都指向了一个 资源.这个 再次说明了如何去做.我已经复制了相同的内容,但仍然没有显示标题.这就是我所做的: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>
以及您的项目依赖项中的以下内容:
And the below in your project dependency:
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>1.3.2</version>
</dependency>
这篇关于Access-Control-Allow-Origin: * 在 tomcat 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!