GeoServer 可以直接安装(exe)或者把 war 包丢到Tomcat,这里的安装方法是后者(丢到Tomcat),本文的跨域设置针对war包丢Tomcat的安装方式,对其他安装方式设置不一定有效。
Tomcat版本:9.0.8
GeoServer版本:2.13.0
各种搜索之后,跨域一般有下面几种方法:
1.下载跨域jar包jetty-servlets.jar
下载jetty-servlets-9.2.13.v20150730.jar跟jetty-util-9.2.13.v20150730.jar两个包(jar包在https://search.maven.org搜索并下载),放到geoserver lib 库里面,即路径:
%TOMCAT_PATH%\webapps\geoserver\WEB-INF\lib(后面简写lib)
然后打开 %TOMCAT_PATH%\webapps\geoserver\WEB-INF\web.xml(后面简写web.xml,所有方法都要修改该文件),把两个注释的地方打开
2.下载cors-filter
同时下载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>
3.下载cors压缩包
下载地址http://shanbe.hezoun.com/cors.zip ,然后解压到 %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>
然而,以上三种方法对我都不起作用
后来找了 Tomcat 的跨域设置:
<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>
将其拷贝到了 geoserver 的 web.xml,不需要引入任何库,重启Tomcat
最后竟然成功了
如果还没有成功,可能 Tomcat 还需要设置跨域,即在 %TOMCAT_PATH%/conf/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>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<!-- <init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
笔者注:禁止跨域其实是为了安全问题,生产环境下不建议设置允许跨域,可通过nginx配置或nodejs转发
nodejs转发,可以见未来的一篇文章点击链接
更多GIS相关开发欢迎关注公众号