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?
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:For you purpose, you may want to use
tileLoadFunction
(API doc) fromol.source.ImageWMS
To illustrate, you can look below. The 2 "secrets" are
customLoader
and for authentication to uncommentreq.setRequestHeader("Authorization", "Basic " + window.btoa(user + ":" + pass));
The answer is mainly borrowed from How to add a http header to openlayers3 requests? but with some adaptations as the syntax provided was not working.