How to specify Authorization Header for a source in mapbox-gl-js?

落花浮王杯 提交于 2020-01-22 15:04:19

问题


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"

The WMS example, map#addSource and map#addLayer lead me to believe it is not possible to set tile request headers.


回答1:


You can now use the transformRequest option to add a custom header:

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 optionally headers and credentials properties.

Example:

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


来源:https://stackoverflow.com/questions/40940860/how-to-specify-authorization-header-for-a-source-in-mapbox-gl-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!