GeoServer 跨域访问
转自 http://blog.csdn.net/mengdong_zy/article/details/51784781
http://www.cnblogs.com/ytwy/p/6823955.html
我们通常需要通过ajax跨域访问GIS服务,GeoServer默认是不支持跨域的,通常直接访问浏览器会报类似:XMLHttpRequest 、not allowed by Access-Control-Allow-Origin的错误提示。
一、GeoServer 2.3.2版本设置
1、下载跨域文件CrossOriginFilter.class并将插件放入<Geoserver>\webapps\geoserver\WEB-INF\classes下
最后形成的文件和目录是:
<Geoserver>\webapps\geoserver\WEB-INF\classes\org\mortbay\servlets\CrossOriginFilter.class
2、设置跨域配置。打开<Geoserver>\webapps\geoserver\web.xml文件,找到文件中<filter>平级的位置,添加如下内容
- …
- <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>
- …
3、重启GeoServer服务。
二、GeoServer 2.10.1版本设置(其他版本皆可使用该通用设置方法)
1、下载跨域jar包jetty-servlets.jar(下载geoserver使用的对应jetty版本)并将jar包放到<Geoserver>\webapps\geoserver\WEB-INF\lib文件夹下。
2、设置跨域配置。打开<Geoserver>\webapps\geoserver\web.xml文件,找到文件中<filter>平级的位置,添加如下内容
- <filter>
- <filter-name>cross-origin</filter-name>
- <filter-class>org.eclipse.jetty.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-mapping>
- <filter-name>cross-origin</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
3、重启GeoServer服务。
三.(亲测可用)
下载cors-filter-1.7.jar,Java-property-utils-1.9.jar这两个库文件,放到<Geoserver>\webapps\geoserver\WEB-INF\lib目录下
下载地址:http://files.cnblogs.com/files/ytwy/jar.rar
打开<Geoserver>\webapps\geoserver\web.xml文件,找到文件中<filter>平级的位置,添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< 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-mapping>平级的位置,添加如下内容:
1
2
3
4
|
< filter-mapping > < filter-name >CORS</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
转载自:https://blog.csdn.net/qq_29176825/article/details/77968307