GeoServer 2.15.0版本跨域问题解决方法

时间:2022-09-20 15:54:17

geoserver默认不开启跨域设置,开启步骤如下:

尝试以下几种方法:

方法一:

1、修改配置文件web.xml,该配置文件的路径如下

\webapps\geoserver\WEB-INF\web.xml

2、搜索:cross-origin,将注释的部分解注。

 <!-- Uncomment following filter to enable CORS -->
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>chainPreflight</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST,PUT,DELETE,HEAD,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>*</param-value>
</init-param>
</filter> <!-- Uncomment following filter to enable CORS -->
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3、下载 jetty-servlets jar包,2.15.0对应的版本是 jetty-servlets-9.4.12.v20180830.jar,放在

\geoserver\WEB-INF\lib

4、重启geoserver,如果报错,再下载对应 jetty-http 和 jetty-util jar包,放在lib目录,再次重启即可,jetty-servlets的jar包要跟jetty-util的jar包版本匹配,jar包下载地址:

https://pan.baidu.com/s/1E9Z4_zGfWWxkJpEZ0yyrPg  ,提取码  d96c

方法二:

同时下载cors-filter-1.7.jar 跟 java-property-utils-1.9.jar (其他版本可能会导致 geoserver 打不开,测试过用最新版本出现geoserver打不开),同样放进 lib 文件夹里面,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>Accept, Origin, X-Requested-With, 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>

方法三:

下载cors压缩包 ,提取码:mdzo ,  然后解压到 %TOMCAT_PATH%\webapps\geoserver\WEB-INF\classes,然后在 web.xml 配置:

<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.mortbay.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>x-requested-with,content-type</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

方法四:

在 geoserver 的 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。

方法五

基于tomcat安装的话,可以试试给tomcat整体配跨域策略

解决方案:

一:在tomcat中的lib文件夹下增加两个jar包

cors-filter-2.4.jar  提取码 eqlq
java-property-utils-1.9.1.jar   提取码  ybz7

二:在tomcat中的conf文件夹下的web.xml文件中的web-app代码下增加如下代码

<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>Accept,Origin,X-Requested-With,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