如何在mapbox-gl-js中为源指定授权标头? [英] How to specify Authorization Header for a source in mapbox-gl-js?
问题描述
如何使用mapbox-gl-js为wms源设置请求标头?我需要所有平铺请求来添加一个类似于以下内容的标头:
How do I set a request header for a wms source with mapbox-gl-js? I need all tile requests to add a header that looks like:
Authorization: "Bearer base64-encoded-token"
WMS示例, map#addLayer 使我认为无法设置图块请求标头.
The WMS example, map#addSource and map#addLayer lead me to believe it is not possible to set tile request headers.
推荐答案
您现在可以使用 transformRequest
选项添加自定义标头:
You can now use the transformRequest
option to add a custom header:
在Map请求外部URL之前运行的回调.回调可用于修改url,设置标头或为跨域请求设置凭据属性.预期返回具有
url
属性以及可选的headers
和credentials
属性的对象.
A callback run before the Map makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests. Expected to return an object with a
url
property and optionallyheaders
andcredentials
properties.
示例:
var map = new mapboxgl.Map({
container: 'map',
center: [2.35, 48.86],
zoom: 13,
transformRequest: (url, resourceType)=> {
if(resourceType == 'Source' && url.startsWith('http://myHost') {
return {
url: url,
headers: { 'Authorization': 'Bearer ' + yourAuthToken }
}
}
}
});
这篇关于如何在mapbox-gl-js中为源指定授权标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!