背景:SuperMap iPortal 上注册的REST服务,设置了访问权限,第三方访问对应服务需要携带key校验。
1、在我的账户下申请key,添加可访问的服务
2、客户端SDK代码实现
1)请求url后直接拼接:xxx?key=xxxx
2)通过引用的SDK默认API添加key
SuperMap.SecurityManager.registerKey(url2,'F4UsN1JMNunpxus4ounA6OIW');
注意设置source时需要设置下当前服务来源:serverType: SuperMap.ServerType.IPORTAL,
<!-- * @Author: your name * @Date: 2021-11-03 12:45:38 * @LastEditTime: 2021-11-17 10:10:14 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \xxx.html --> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title data-i18n="resources.title_WMSLayer"></title> <script type="text/javascript" src="../js/include-web.js"></script> <script type="text/javascript" src="../../dist/ol/include-ol.js"></script> </head> <body style=" margin: 0;overflow: hidden;background: #fff; 100%;height:100%; position: absolute;top: 0;"> <div id="map" style=" 100%;height:100%"></div> <script type="text/javascript"> var map; map = new ol.Map({ target: 'map', controls: ol.control.defaults({ attributionOptions: { collapsed: false } }) .extend([new ol.supermap.control.Logo()]), view: new ol.View({ center: [104.09 , 30.52], zoom: 16, projection: 'EPSG:4326', multiWorld: true }), layers: [new ol.layer.Tile({ source: new ol.source.Tianditu({ layerType: 'img', key: "1d109683f4d84198e37a38c442d68311", projection: "EPSG:4326" }) }), new ol.layer.Tile({ source: new ol.source.Tianditu({ layerType: 'img', key: "1d109683f4d84198e37a38c442d68311", isLabel: true, projection: "EPSG:4326" }) })] }); var url2="http://xxx:8195/portalproxy/iserver/services/map-CT/rest/maps/outdoor_map_cn"; SuperMap.SecurityManager.registerKey(url2,'F4UsN1JMNunpxus4ounA6OIW'); var layer = new ol.layer.Tile({ source: new ol.source.TileSuperMapRest({ serverType: SuperMap.ServerType.IPORTAL, url: url2, //url: "http://xxx:8195/portalproxy/iserver/services/map-CT/rest/maps/outdoor_map_cn", //url: "http://xxx:8195/portalproxy/iserver/services/map-CT/rest/maps/outdoor_map_cn?key=F4UsN1JMNunpxus4ounA6OIW", wrapX: true }), projection: 'EPSG:4326' }); serverType:SuperMap.ServerType.IPORTAL map.addLayer(layer); map.addControl(new ol.supermap.control.ScaleLine()); </script> </body> </html>