如何在mapbox-gl-js中为源指定授权标头? [英] How to specify Authorization Header for a source in mapbox-gl-js?

查看:482
本文介绍了如何在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示例

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属性以及可选的headerscredentials属性的对象.

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.

示例:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆