ArcGIS api for javascript 通过使用代理的方式访问enterprise的安全服务

      当ArcGIS enterprise中的服务没有设置共享属性或未公开时,前端在访问该服务的时候就会出现输入用户名和密码的弹框,并且每次刷新网页的时候,都需要手动登录一次Portal,但是对于多数用户来说觉着这样很不方便,那如何才能不显示这个登录框呢?下面将介绍另一种方式即通过使用代理的方式来实现。(:本篇所介绍的方法适用场景是arcgis server和portal进行了联合,server由portal进行托管,server服务的权限、访问是通过portal来进行管理)
在这里插入图片描述
ArcGIS API for JavaScript4.x中代理页面配置的说明
https://developers.arcgis.com/javascript/latest/guide/proxies/
ArcGIS API for JavaScript3.x中代理页面配置的说明
https://developers.arcgis.com/javascript/3/jshelp/ags_proxy.html

具体配置过程:
1、在GitHub上下载Esri提供的代理应用资源 ,代理的下载地址及使用见 https://github.com/Esri/resource-proxy/releases
2、针对不同的平台有不同的代理应用分布为IIS、Java(Tomcat )、PHP,大家可以根据自己的环境进行选择。这里我以IIS为例进行代理部署,下载并解压缩.zip文件之本地,直接将DotNet文件夹放到wwwroot目录下。这里我将其文件夹重命名为proxy。
3、右键proxy文件夹,选择“转换为应用程序”,应用程序池至少为4.0,如下图
在这里插入图片描述
在这里插入图片描述
4、测试代理是否已安装并可用:
http://[yourmachine]/proxy/proxy.ashx?ping
使用以下命令测试代理是否可以在浏览器中直接转发请求:
http://[yourmachine]/proxy/proxy.ashx?http://services.arcgisonline.com/ArcGIS/rest/services/?f=pjson
5、配置代理文件,需要编辑下proxy.config文件以设置代理配置,各参数的详细参照:https://github.com/Esri/resource-proxy/blob/master/README.md#proxy-configuration-settings
在这里插入图片描述
6、使用代理:
      (1) 可以使用特定的代理规则配置您的应用程序。当应用程序尝试通过此URL访问资源时,请求将通过指定的代理发送。该请求的proxyRules属性是一个对象,列出所有这些代理规则。使用urlUtils.addProxyRule()

api 4.x

require(["esri/core/urlUtils"], function(urlUtils) {
  urlUtils.addProxyRule({
    urlPrefix: "https://wl.arcgisonline.cn",
    proxyUrl: "https://wl.arcgisonline.cn/proxy/proxy.ashx"
  });
});

api 3.x

require(["esri/urlUtils"], function(urlUtils) {
 urlUtils.addProxyRule({
  urlPrefix: "https://wl.arcgisonline.cn",
  proxyUrl: "https://wl.arcgisonline.cn/proxy/proxy.ashx"
});
 });

(2) 如果应用程序中的所有请求都使用相同的代理,也可以使用请求对象的proxyUrl属性指定位置。

api 4.x:

require(["esri/config"], function(esriConfig) {
  esriConfig.request.proxyUrl = "https://wl.arcgisonline.cn/proxy/proxy.ashx";
});

api 3.x

require(["esri/config"], function(esriConfig) { 
esriConfig.defaults.io.proxyUrl = "https://wl.arcgisonline.cn/proxy/proxy.ashx"
esriConfig.defaults.io.alwaysUseProxy = true;
});

7、完整示例代码
arcgis api for javascript 4.x:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta
            name="viewport"
            content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <title>通过使用代理配置访问portal私有服务</title>
    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.14/esri/css/main.css"/>
    <script src="https://js.arcgis.com/4.14/init.js"></script>
    <script>
        require([
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/core/urlUtils"
        ], function(
            Map,
            MapView,
            FeatureLayer,
            urlUtils
        ) {
            // Proxy the route requests to avoid prompt for log in
            urlUtils.addProxyRule({
                urlPrefix: "wl.arcgisonline.cn",
                proxyUrl: "https://wl.arcgisonline.cn/proxy/proxy.ashx"
            });
            var map = new Map({
                basemap: "osm"
            });
            var view = new MapView({
                container: "viewDiv",
                map: map,
                zoom: 13,
                center: [116.38, 39.9]
            });
            var featureLayerUrl =
"https://wl.arcgisonline.cn/server/rest/services//wl/time1/MapServer/0";
            featureLayer = new FeatureLayer({
                url: featureLayerUrl,
                outFields: ["*"],
            });
            map.add(featureLayer); 
        });
    </script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>

arcgis api for javascript 3.x:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>test</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.31/dijit/themes/tundra/tundra.css">
    <link rel="stylesheet" href="https://js.arcgis.com/3.31/esri/css/esri.css">
    <style>
        html, body, #mapDiv {
            padding:0;
            margin:0;
            height:100%;
        }
    </style>

    <script src="https://js.arcgis.com/3.31/"></script>
    <script>
        require(["esri/config",
            "esri/map", "esri/layers/FeatureLayer",
             "dojo/domReady!"
        ], function(esriConfig,
            Map, FeatureLayer,
        ) {
            esriConfig.defaults.io.proxyUrl = "https://wl.arcgisonline.cn/proxy/proxy.ashx";
            esriConfig.defaults.io.alwaysUseProxy = true;
            var map = new Map("mapDiv", {
                basemap: "osm",
                center: [112, 33.646],
                zoom: 8
            });
            var featurelayer = new FeatureLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3", {
                mode: FeatureLayer.MODE_SNAPSHOT,
                outFields: ["*"]
            });
            map.addLayer(featurelayer);

        });
    </script>
</head>
<body class="tundra">
<div id="mapDiv">
</div>
</body>
</html>

此时当我们调用portal中的私有服务时不再弹出登录框。

发布了8 篇原创文章 · 获赞 0 · 访问量 692

猜你喜欢

转载自blog.csdn.net/qq_40376439/article/details/104217511