本文介绍了如何在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#addSource 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:

示例:

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中为源指定授权标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 10:32