对Geoserver2.4.8的跨域访问 CORS

首先,先讲一下开发环境:

openlayers框架是在satMCS工程中使用,用到的服务器是jetty 9.0;localhost:8080

Geoserver部署在jetty 6.1.8的服务器上。localhost:8090

当openlayers通过WFS来访问Geoserver上的数据时,希望通过Ajax方式来获取数据.

Tomcat6 配置cgi openlayers跨域访问 
本篇将的是如何用tomcat + CGI 来设置代理实现跨域访问。

该篇用到的是另外一种方法:CORS (cross-origin-resource-sharing)

1.首先在Geoserver的工程路径下导入一个包:http://shanbe.hezoun.com/cors.zip    (如果由于是国外的网址无法下载,请尝试用合理的方法下载该文件)

  工程路径:(D:\Program Files\Coding Software\GeoServer 2.4.8\webapps\geoserver\WEB-INF\classes)

  解压该包到classes文件夹下:

2.配置Geoserver工程的web.xml文件:

该文件路径:D:\Program Files\Coding Software\GeoServer 2.4.8\webapps\geoserver\WEB-INF

在该文件中,做以下两处修改:

(1)在filter集合末尾额外加一个filter,代码如下:

<!--This filter is based on GIS ChinaMap -->
    <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> 

在web.xml的配置文件中,我们可以配置下面这些属性:

  • allowedOrigins, a comma separated list of origins that areallowed to access the resources. Default value is
    *, meaning allorigins
  • allowedMethods, a comma separated list of HTTP methods thatare allowed to be used when accessing the resources. Default value isGET,POST
  • allowedHeaders, a comma separated list of HTTP headers thatare allowed to be specified when accessing the resources. Default valueis
    X-Requested-With
  • preflightMaxAge, the number of seconds that preflight requestscan be cached by the client. Default value is
    1800 seconds, or 30minutes
  • allowCredentials, a boolean indicating if the resource allowsrequests with credentials. Default value is
    false

(2)在filter-mapping集合末尾额外加一个filter-mapping,代码如下:

<!--ChinaMap -->
    <filter-mapping>
        <filter-name>cross-origin</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

至此,配置完毕,即可利用CORS来跨域访问:

另外:用firebug抓取的包如上图所示。

其中有几个属性可以稍微注意一下:

其中最敏感的就是 AccessControlAllowOrigin 这个Header, 他是W3C标准里用来检查该跨域请求是否可以被通过.

跨域实现的过程大致如下:(部分参考:http://huaidan.org/archives/2729.html)

http://www.a.com/test.html 发起一个跨域请求,

请求的地址为: http://www.b.com/test.php

如果 服务器B返回一个如下的header

Access-Control-Allow-Origin: http://www.a.com

那么,这个来自 http://www.a.com/test.html 的跨域请求就会被通过。

在这个过程中, request 还会带上这个header:

Origin: http://www.a.com

不过这里比较要命的是 Access-Control-Allow-Origin 的值可以是通配符 *

如果是 * 的话,就可以接收来自任意source origin的请求。

也可以参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

origin主要是用来说明最初请求是从哪里发起的;
origin只用于Post请求
Referer则用于所有类型的请求;
origin的方式比Referer更安全点吧。
Host:代表访问的目标地址
x-request-with:XMLHttpRequest代表是用ajax方式发起请求的

转载自:https://blog.csdn.net/Lin00Kun11/article/details/39157411

You may also like...

退出移动版