Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openlayers 4 - Load WMS image layer require authentication

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'
    });
});

But it throws an error:

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

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

like image 792
abd0991 Avatar asked Feb 13 '26 03:02

abd0991


1 Answers

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

This is the tileLoader function that worked for me:

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();
}
like image 169
abd0991 Avatar answered Feb 15 '26 18:02

abd0991



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!