详细解析在安卓端访问arcgis enterprise加密服务2-场景1

0
分享 2020-02-11
场景一是结合使用user login 和oauth两个方式来实现安卓端访问加密服务。

场景一适用的要求:portal 中的数据保密,将其设置为有限的人员可见。

环境满足条件:一套现有的arcgis enterpise,且给用户分配了账户。

整体实现逻辑包含两部分:
  1. 在arcgis enterprise中注册你的开发程序;
  2. 在代码中实现验证oauth。

 
------------------------------------------------------------
 
在arcgis enterprise中注册你的开发程序,具体步骤如下:
首先,需要给你的应用设置oauth2。
1.注册你的应用程序生成一个client ID, 并且设置一个转发URL用于访问安全服务:
1.1登录arcgis enterprise;
1.2点击   “添加项目(add item)” 创建一个新应用。
 

Image1.png

                                                            P1
 

1.3填写新应用的详细信息,然后选择注册新的应用。可以留意到一个client ID自动生成并分配给你的应用。
1.4注册完成后,在新生成的item属性页面,执行注册应用。其中关键的参数是redirect uri, 此处填写的参数要带入安卓程序中使用。此处填写的是 my-app://auth。其中my-app可以根据自己的命名规则改写。
 

Imag2e.png

                                                         p2
 
注册完成后,页面会提供app id和redirect uri两个关键参数。
 

Image3.png

                                                                  p3
 
执行完以上步骤就完成了注册应用,关键参数也已经拿到,可以继续在代码中实现oauth验证了。
 
------------------------------------------------------------

在代码中实现验证oauth:
2 创建一个app_setting.xml 资源文件,里面添加client ID 和定义的rediect url:
2.1 在工程视图下,右键app,选择new - Android resource file.
2.2 将文件名设置为app_settings.xml;
2.3 将注册过程中产生的client id 和 redirect URI放在文件中,文件内容看起来如下
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- *** ADD *** -->
<string name="redirect_url">my-app://auth</string>
<string name="client_id">CFDhh4i0RJaXz4pz</string>
</resources>

3 在程序主要的java代码中实现以下关键代码:
 
private void setupOAuthManager() 
{ String clientId = getResources().getString(R.string.client_id); 
String redirectUrl = getResources().getString(R.string.redirect_url); 

try {
#将下
https://www.arcgis.com替换成你环境中的portal地址。我的代码中portal地址为我本地的地址:https://ggxl.xxxx.com/portal


 OAuthConfiguration oAuthConfiguration = new OAuthConfiguration("https://www.arcgis.com", clientId, redirectUrl);
 DefaultAuthenticationChallengeHandler authenticationChallengeHandler = new DefaultAuthenticationChallengeHandler(this); AuthenticationManager.setAuthenticationChallengeHandler(authenticationChallengeHandler); 
AuthenticationManager.addOAuthConfiguration(oAuthConfiguration); } catch (MalformedURLException e) { e.printStackTrace(); } }

------------------------------------------------------------

以上就完成了在安卓端使用name user login的方法,在服务器端通过oauth方法验证登录,访问加密服务了。成功后会在安卓端看到如下的登录界面:
 

Image4.png

                                                                P4


------------------------------------------------------------
参考帮助连接:
  1. https://github.com/Esri/arcgis-runtime-samples-android/tree/master/java/authenticate-with-oauth
  2. https://developers.arcgis.com/labs/android/access-services-with-oauth-2/

0 个评论

要回复文章请先登录注册