环境
- Linux CentOS 7.5
- tomcat 9.0.33
- geoserver 2.15.2
- geowebcache 1.10.0
操作步骤
Geoserver有多个跨域解决方法,在各个环境下有的有效有的无效,具体原因未知,请客官们自行尝试。在此只是对用到过成功的案例做个记录。
- 文中使用到的资源下载地址(jar包):
https://download.csdn.net/download/guzicheng1990/12300148 - 也可以maven官网自行找对应版本下载:
https://search.maven.org/
方法一(独立bat版现用方法)
-
找到geoserver配置文件/geoserver/WEB-INF/web.xml
-
文档中搜索关键词'cross-origin',发现web.xml中有两处被注释的节点,放开注释。
-
找到jetty-servlets-9.4.12.v20180830.jar,将其复制到/geoserver/WEB-INF/lib目录底下
-
重启Geoserver。这时候Geoserver可能会启动不了,报错503(Problem accessing /geoserver/web/. Reason: Service Unavailable Powered)
-
这时再下载对应 jetty-http-9.4.12.v20180830.jar 和 jetty-util-9.4.12.v20180830.jar jar包,放在WEB-INF/lib目录,再次重启即可(注意:jetty-servlets的jar包要跟jetty-util及jetty-http的jar包版本匹配)
-
重启geoserver
方法二
** 方法二与方法五类似,方法二是放在geoserver的lib下,方法五是放在tomcat的lib下。区别是独立版和tomcat版部署方式 **
-
同时下载cors-filter-1.7.jar 跟 java-property-utils-1.9.jar (其他版本可能会导致 geoserver 打不开,测试过用最新版本出现geoserver打不开),放进 /geoserver/WEB-INF/lib 文件夹里面
-
/geoserver/WEB-INF/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>
方法三
-
下载跨域文件CrossOriginFilter.class,然后解压到 geoserverWEB-INFclasses,保持原有路径(geoserverWEB-INFclassesorgmortbayservletsCrossOriginFilter.class)
-
然后在 /geoserver/WEB-INF/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>
方法四
- 编辑 geoserverWEB-INFweb.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进行跨域
-
将cors-filter-2.4.jar 和 java-property-utils-1.9.1.jar 包放到 /tomcat/lib 下
-
修改/tomcat/conf/下的web.xml文件,在
<web-app></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>