Openlayers 4 - 加载 WMS 图像层需要身份验证
Openlayers 4 - Load WMS image layer require authentication
我尝试用openlayers 4.6和angular5加载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 (An Authentication object was not found in the
SecurityContext)
如何使用 openlayers 发送的 GET 请求发送身份验证 header?
为了您的目的,您可能需要使用 ol.source.ImageWMS
中的 tileLoadFunction
(API doc)
为了说明,你可以看下面。 2 个“秘密”是 customLoader
和用于取消注释的身份验证 client.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
//client.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>
答案主要是从 借来的,但由于提供的语法无效而进行了一些改编。
谢谢@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();
}
我尝试用openlayers 4.6和angular5加载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 (An Authentication object was not found in the SecurityContext)
如何使用 openlayers 发送的 GET 请求发送身份验证 header?
为了您的目的,您可能需要使用 ol.source.ImageWMS
tileLoadFunction
(API doc)
为了说明,你可以看下面。 2 个“秘密”是 customLoader
和用于取消注释的身份验证 client.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
//client.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>
答案主要是从
谢谢@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();
}