使用代理请求Geoserver服务

1、使用代理的目的

  1. 当使用Geoserver作为GIS后台服务开发WebGIS应用时,如果在外网环境中,那么Geoserver也必须部署在外网环境中。Geoserver负责图层发布与修改,暴露在外网环境下,存在一定的安全隐患。因此使用代理的方式间接访问Geoserver,使Geoserver部署在局域网内,可以提高安全性。这和数据库一般部署在局域网环境下是相同的道理。
  2. Geoserver的WFS请求不允许跨域,虽然可以通过JSONP、CORS等方式实现跨域请求,使用代理请求WFS仍然是一个不错的方式。

2、设计代理层的思路

  1. 应兼容Geoserver的WMS、WFS等协议格式;
    2)前端页面无须做额外的修改。
    下述代码中,前端页面将geoserver的地址修改为代理层地址即可:
 var partLayerTiled = new ol.layer.Tile({
            visible: true,
            source: new ol.source.TileWMS({
                url:  '/GeoserverWmsProxy.ashx', // 'http://localhost:8080/geoserver/szgas/wms',
                params: {
                    'FORMAT': format,
                    'VERSION': '1.1.1',
                    tiled: true,
                    STYLES: '',
                    LAYERS: 'szgas:MVALVE',//部件图层
                },
            }),
            //从第4级开始加载部件图层;
            maxResolution: resolutions[3],
        });

3、代理层参考代码(c#实现)

代理层基类:

public abstract class GeoserverBaseProxy : IHttpHandler
    {
        protected abstract string GeoserverUrl
        {
            get;
        }

        public void ProcessRequest(HttpContext context)
        {
            string targetUrl = GeoserverUrl;
            for (int i = 0, count = context.Request.QueryString.Count; i < count; i++)
            {
                targetUrl += context.Request.QueryString.Keys[i] + "=" + context.Request.QueryString.Get(i) + "&";
            }
            HttpWebRequest targetRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
            targetRequest.UserAgent = context.Request.UserAgent;
            targetRequest.ContentType = context.Request.ContentType;
            targetRequest.Method = context.Request.HttpMethod;

            byte[] buffer = new byte[8 * 1024];
            int bufferLength = 8 * 1024;
            int ret = 0;
            if (targetRequest.Method.ToUpper() == "POST")
            {
                Stream targetInputStream = targetRequest.GetRequestStream();
                ret = context.Request.InputStream.Read(buffer, 0, bufferLength);
                while (ret > 0)
                {
                    targetInputStream.Write(buffer, 0, ret);
                    ret = context.Request.InputStream.Read(buffer, 0, bufferLength);
                }
                targetInputStream.Close();
            }
            HttpWebResponse targetResponse = (HttpWebResponse)targetRequest.GetResponse();
            context.Response.ContentType = targetResponse.ContentType;

            Stream targetOutputStream = targetResponse.GetResponseStream();
            Stream sourceOutputStream = context.Response.OutputStream;
            ret = targetOutputStream.Read(buffer, 0, bufferLength);
            while (ret > 0)
            {
                sourceOutputStream.Write(buffer, 0, ret);
                ret = targetOutputStream.Read(buffer, 0, bufferLength);
            }
            targetResponse.Close();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

WMS服务代理:

    public class GeoserverWmsProxy : GeoserverBaseProxy
    {
        const string __geoserverUrl = "http://localhost:8080/geoserver/szgas/wms?";

        protected override string GeoserverUrl
        {
            get
            {
                return __geoserverUrl;
            }
        }
    }

WFS服务代理:

 public class GeoserverWfsProxy : GeoserverBaseProxy
    {
        const string __geoserverUrl = "http://localhost:8080/geoserver/szgas/wfs?";

        protected override string GeoserverUrl
        {
            get
            {
                return __geoserverUrl;
            }
        }
       
    }

Geowebcache切片服务代理:

 public class GeoserverGwcProxy : GeoserverBaseProxy
    {
        const string __geoserverUrl = "http://localhost:8080/geoserver/gwc/service/wms?";

        protected override string GeoserverUrl
        {
            get
            {
                return __geoserverUrl;
            }
        }          
    }

请将上述代码中Geoserver的地址修改为读取配置项。

推荐

F3Earth是一群志同道合的伙伴发起的国产Web 3D Engine项目,github地址:https://github.com/f3earth/f3earth , 目前正在开发中,DEMO已初具功能,期待更多朋友的参与。

你可能感兴趣的:(使用代理请求Geoserver服务)