我有一个运行在 Tomcat 之上的 GeoServer 应用程序。我想要的是设置一个额外的响应 header - Access-Control-Allow-Origin: *。我需要这个,因为现在我无法在我的 map 应用程序中实现功能,因为在浏览器中我得到了
The operation is insecure
留言
根据 this线程,我需要设置此 header :"Access-Control-Allow-Origin: *"
并根据 this Tomcat 中的线程我可以通过 CATALINA_HOME/conf/web.xml 设置它:
<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>
我确实这样做了,重新启动了 Tomcat,但我仍然没有看到该 header 作为响应。这是服务器对客户端的响应:
那么,我该如何解决呢?如何强制我的 GeoServer 应用程序(第三方应用程序)以 "Access-Control-Allow-Origin: *"
响应?
编辑
我应该补充一点,我使用的是 Tomcat 8。此外,我的过滤器现在看起来像:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我将此过滤器添加到主 web.xml 和应用程序的 web.xml,但无济于事。因此,似乎以前所有针对此问题的解决方案都已过时。
最佳答案
我刚刚遇到了同样的问题,我的解决方案是添加您在地理服务器 web.xml
这足以解决问题。无需更改 Tomcat 中的任何内容
就这样吧:
CORS * for GeoServer with Tomcat 将以下内容添加到 /var/lib/tomcat7/webapps/geoserver/WEB-INF/web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
最后重启tomcat (sudo systemctl restart tomcat.service
)
关于tomcat - 在 Tomcat 中添加响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146530/