Openlayers 4-加载WMS图像层需要身份验证 [英] Openlayers 4 - Load WMS image layer require authentication

查看:299
本文介绍了Openlayers 4-加载WMS图像层需要身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to load WMS image layer with openlayers 4.6 and angular 5, the code is:

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(在 SecurityContext)

GET (myserverurl) 401 (An Authentication object was not found in the SecurityContext)

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

How can I send authentication header with the GET request sent by openlayers?

推荐答案

感谢@Thomas,您的答案不是100%正确,但是它为我提供了获得正确答案的途径.

Thanks @Thomas, your answer isn't correct 100% but it clear the way for me to get the correct answer.

这是对我有用的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();
}

这篇关于Openlayers 4-加载WMS图像层需要身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆