【问题标题】:Openlayers 4 - Load WMS image layer require authenticationOpenlayers 4 - 加载 WMS 图像层需要身份验证
【发布时间】:2018-05-22 15:37:12
【问题描述】:

我尝试用 openlayers 4.6 和 angular 5 加载 WMS 图像层,代码是:

const syr_layer = new ol_layer_Image({
  source: new ol_source_ImageWMS({
      url: 'serverurl', crossOrigin: 'anonymous', serverType: 'geoserver',
      params: { 'LAYERS': 'tst:syr'},
      projection: 'EPSG:4326'
    });
});

但是它会抛出一个错误:

GET (myserverurl) 401(在 安全上下文)

如何通过 openlayers 发送的 GET 请求发送身份验证标头?

【问题讨论】:

    标签: typescript authentication openlayers wms


    【解决方案1】:

    感谢@Thomas,您的答案不是 100% 正确,但它为我获得正确答案扫清了道路。

    这是对我有用的 tileLoader 函数:

    private tileLoader(tile, src) {
      const client = new XMLHttpRequest();
    
      client.open('GET', src);
      client.responseType = 'arraybuffer';
      client.setRequestHeader('Authorization', 'Basic ' + btoa(user + ':' + pass));
    
      client.onload = function () {
        const arrayBufferView = new Uint8Array(this.response);
        const blob = new Blob([arrayBufferView], { type: 'image/png' });
        const urlCreator = window.URL || (window as any).webkitURL;
        const imageUrl = urlCreator.createObjectURL(blob);
        tile.getImage().src = imageUrl;
      };
    
      client.send();
    }
    

    【讨论】:

      【解决方案2】:

      出于您的目的,您可能希望使用来自ol.source.ImageWMS 的tileLoadFunction (API doc)

      为了说明,你可以看下面。 2个“秘密”是customLoader,用于身份验证以取消注释req.setRequestHeader("Authorization", "Basic " + window.btoa(user + ":" + pass));

      <!DOCTYPE html>
      <html>
        <head>
          <title>Tiled WMS</title>
          <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
          <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
          <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
          <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
        </head>
        <body>
          <div id="map" class="map"></div>
          <script>
            function customLoader(tile, src) {
              var client = new XMLHttpRequest();
              client.open('GET', src);
              // Uncomment to pass authentication header
              //req.setRequestHeader("Authorization", "Basic " + window.btoa(user + ":" + pass));
              client.onload = function() {
                tile.getImage().src = src;
              };
              client.send();
            }
            var layers = [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              }),
              new ol.layer.Tile({
                extent: [-13884991, 2870341, -7455066, 6338219],
                source: new ol.source.TileWMS({
                  url: 'https://ahocevar.com/geoserver/wms',
                  tileLoadFunction: customLoader,
                  params: {'LAYERS': 'topp:states', 'TILED': true},
                  serverType: 'geoserver',
                  // Countries have transparency, so do not fade tiles:
                  transition: 0
                })
              })
            ];
            var map = new ol.Map({
              layers: layers,
              target: 'map',
              view: new ol.View({
                center: [-10997148, 4569099],
                zoom: 4
              })
            });
          </script>
        </body>
      </html>
      

      答案主要是从How to add a http header to openlayers3 requests?借来的,但由于提供的语法不起作用,所以做了一些修改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-26
        • 1970-01-01
        • 2015-04-11
        • 2012-11-13
        相关资源
        最近更新 更多