ArcGIS API For Python - 使用不同的身份验证方案

ArcGIS API For Python中的GIS对象提供了对于几类用户的多种验证方案:

  • 匿名用户
  • 内置用户
  • Enterprise标识存储
    • Web-tier authentication with LDAP
    • Portal-tier authentication with Active Directory
    • Portal-tier authentication with LDAP
    • Web-tier authentication secured with PKI
    • Web-tier authentication secured with IWA
  • 基于OAuth 2.0的用户身份验证
  • 通过ArcGIS Pro连接

GIS类支持通过向ArcGIS Online或者ArcGIS Enterprise实例提供URL或者用户凭据的方式构建GIS对象。用户凭据内容可包括一对用户名和用户密码,或者在使用PKI的情况下,使用一个密钥文件/文件证书对作为用户凭据内容。

如果不提供URL地址,默认使用ArcGIS Online;如果不提供用户凭据,则默认以匿名用户访问。另外,脚本可通过ArcGIS Pro登录Portal,并且会选择使用当前激活的Portal。

匿名用户访问

作为匿名用户访问此API,你可完成的任务比较少,可以查询或者查看公开分享的地图和图层,但是,向创建或者修改内容、执行分析任务等情况是没有权限的。

  • 作为匿名用户连接ArcGIS Online
print("ArcGIS Online as anonymous user")
gis = GIS()
print("Logged in as anonymous user to " + gis.properties.portalName)
  • 作为匿名用户连接ArcGIS Enterprise

如果ArcGIS Enterprise允许匿名用户访问,只传入Enterprise的URL地址,不传入用户名和密码,即可以匿名用户的方式连接Enterprise。

值得注意的一点,如果Enterprise配置了IWA认证,并且在Windows系统中登录Enterprise,可能连接时会自动登录。

print("ArcGIS Enterprise as anonymous user")
gis = GIS("https://singlega.jiaoxn.local/arcgis")
print("Logged in as anonymous user to " + gis.properties.portalName)

内置用户

无论ArcGIS Online还是ArcGIS Enterprise都使用内置的身份存储进行了预配置,所以可以很轻松的创建用户和创建组。当使用这种情况登录时,需提供用户名和密码。

  • 作为内置用户连接ArcGIS Online
print("ArcGIS Online Org account")    
gis = GIS("https://www.arcgis.com", "arcgis_python", "P@ssword123")
print("Logged in as " + str(gis.properties.user.username))
  • 作为内置用户连接Enterprise
print("Portal for ArcGIS as a built in user")
gis = GIS("https://portalname.domain.com/webadapter_name", "sharinguser", "password")
print("Logged in as: " + gis.properties.user.username)

Enterprise 标识存储

ArcGIS Online和ArcGIS Enterprise的设计允许您通过Enterprise账号和群组来控制对ArcGIS组织的访问。

  • Web-tier authentication with LDAP
print("\n\nBasic Authentication with LDAP")    
ldapbasic = GIS("https://portalname.domain.com/webadapter_name", "amy", "password")
print("Logged in as: " + ldapbasic.properties.user.username)
  • Portal-tier authentication with Active Directory

当使用来自当前激活目录的Enterprise用户连接时,以domain\\username的方式指定用户名。

print("\n\nPortal-tier Authentication with LDAP - enterprise user")
gisldap = GIS("https://portalname.domain.com/webadapter_name", "AVWORLD\\Publisher", "password")
print("Logged in as: " + gisldap.properties.user.username)
  • Portal-tier authentication with LDAP

当使用来自LDAP的Enterprise用户账号连接时,向平常一样指定用户名和密码即可。

print("\n\nPortal-tier Authentication with LDAP - builtin user")    
gisldap = GIS("https://portalname.domain.com/webadapter_name", "sharing1", "password")
print("Logged in as: " + gisldap.properties.user.username)
  • Web-tier authentication secured with PKI

使用PKI连接时,有两种方式:一种是使用key_filecert_file,另一种是使用pkcs12格式化的证书文件和密码

# 使用PEM编码的证书和密钥文件
print("\n\nPKI with key and cert files")  
gis = GIS("https://portalname.domain.com/webcontext", 
          key_file="C:\\path\\to\\key.pem",
          cert_file="C:\\path\\to\\cert.pem")
print("Logged in as: " + gis.properties.user.username)

# 使用PKCS12格式化的证书文件和密码
print("\n\nPKI with PFX file and password")  
gis = GIS("https://portalname.domain.com/webcontext", 
          cert_file="C:\\path\\to\\mycert.pfx",
          password="secret.password")
print("Logged in as: " + gis.properties.user.username)
  • Web-tier authentication secured with IWA

基于OAuth 2.0的用户身份验证

使用此方式连接时,需要一个client.id

获取一个client id

  1. 登录ArcGIS Online或者ArcGIS Enterprise
  2. 选择Content
  3. 点击+ Add ItemAn Application按钮
  4. 添加一个应用:类型为Application,标题为Python,标签为Python
  5. 在刚刚创建的应用的详细页面,导航至Settings
  6. 点击页面底部的Registered Info
  7. 点击后,当前页面会显示一个应用ID,这个就是连接GIS对象时需要的client.id。

此方式连接时,如果不提供用户名和密码,将会显示交互式的登录页面,登录后将会获得一个代码,可以粘贴为该代码已完成登录过程;提供用户名和密码,可屏蔽此过程。

gis = GIS("https://pythonapi.playground.esri.com/portal", username="arcgis_python", password="amazing_arcgis_123",
          client_id='f8cRxbP3NO8bf9ag')
print("Successfully logged in as: " + gis.properties.user.username)

通过ArcGIS Pro连接

基于pro的授权模式,Python代码脚本可以在不输入用户名和密码的情况下,创建代表当前激活的Portal的GIS类实例。

值得注意的是,如果使用Named User license这样的授权方式授权Pro,如果在登录时没有勾选Signe me in automatically或者没有设置Pro可离线使用,则在使用该模式连接时,需确保ArcGIS Pro在运行该代码的机器上已安装并且Pro已运行。如果勾选Signe me in automatically,默认情况下,在Portal许可到期之前,ArcGIS Pro可以连续关闭2周。

print("\n\nActive Portal in ArcGIS Pro")  
gis = GIS("pro")

保护密码

如果向其他用户分享你的Notebook代码或者在你的代码存储在公共位置时,有时会不想直接暴露密码在代码中,可以不输入代码来解决这个问题。当Python API运行代码时,会调用内置模块getpass,提示用户输入代码,如下代码所示。

gis = GIS("https://pythonapi.playground.esri.com/portal", username='arcgis_python')
print("Successfully logged in as: " + gis.properties.user.username)

# Output
Enter password: ········
Successfully logged in as: arcgis_python

当提示输入密码时,代码将会暂停运行,直到用户输入正确的代码。输入的代码会用*号掩盖。

在需要输入密码的连接模式中,均可使用这种模式保护你的密码。如果在单独的Python脚本中使用这种方式时,该脚本需要以交互式的方式运行,因为在脚本运行时,需要用户提供密码。

本地保存资格证书

如果经常连接一个GIS实例,并且考虑将连接证书保存到本地,那么GIS实例化时传入profile参数可解决。

通过向GIS类传递授权凭据,并指定一个证书名称,即可创建持久化的配置文件。这个配置文件在用户主目录下,将除密码以外的授权凭据存储在一个未加密的名为.arcgisprofile配置文件中。资格证书会通过keyring模块以安全的方式将密码存放在操作系统的密码管理器中,如果是Linux系统可能需要安装额外的软件来保证适当的安全性。

保存配置文件后,可通过profile参数传递证书文件。多个证书文件可以被平行的创建和调用。

# 创建
playground_gis = GIS(url="https://pythonapi.playground.esri.com/portal", username='arcgis_python', password='amazing_arcgis_123',
                     profile='python_playground_prof')
agol_gis = GIS(url="https://arcgis.com/", username='arcgis_python', password="P@ssword123",
    profile="AGOL_prof")
print("profile defined for {}".format(playground_gis))
print("profile defined for {}".format(agol_gis))

# 使用

def print_profile_info(gis):
    print("Successfully logged into '{}' via the '{}' user".format(
           gis.properties.portalHostname,
           gis.properties.user.username)) 

playground_gis = GIS(profile='python_playground_prof')
print_profile_info(playground_gis)

agol_gis = GIS(profile="AGOL_prof")
print_profile_info(agol_gis)

持久配置文件详细内容

持久配置文件将在未加密的.arcgisprofile文件中存储以下GIS参数的任意组合:

  • url
  • username
  • key_file
  • cert_file
  • cliend_id

密码将会通过keyring模块以安全的方式的存储,也就是说,在运行代码时,密码将会从操作系统的密码存储管理器中检索。

Windows

Window系统中,密码将会存放在Windows凭据管理器。其查看方式为:

Start > Control Panel > User Accounts > Credential Manager > Windows Credentials > Generic Credentials
Mac

在Mac系统中,密码将会存放在Keychain Access,查看方式为:

Applications > Utilities > Keychain Access > Passwords > "arcgis_python_api_profile_passwords"
Linux

在Linux上,可能需要安装并配置其他软件,以安全地存储具有持久配置文件的密码。其配置过程可通过另外一篇博客来了解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章