启用token
1、登录ArcGIS Server Manager:http://192.168.23.100:8099/arcgismanager/
2、点击Security、Roles,创建角色。
3、点击Security、Users,创建用户,并设置该用户属于创建的角色。
4、点击Security、Settings,在Security for GIS Services,勾选Enable Security for GIS Services,选择ArcGIS Token Service Authentication、Use token service running on this instance of ArcGIS Server,点击Configure。Shared key for encrypting token可以任意输入,点击Ok。最后点击Save即可。
5、点击Services、Manage Services,点击服务后面的锁图标,把角色添加到Allowed Rules中,点击Save。也可以点击工具栏上的锁图标,把角色添加到Allowed Rules中,可以设置所有服务允许的角色。
6、ArcGIS Server提供了工具,可以在浏览器中生成Token:http://192.168.23.100:8399/arcgis/tokens
禁用token
1、停止ArcGIS server object manager (SOM) 进程。
2、使用文本编辑器,打开
3、启动SOM服务。
4、 登录ArcGIS Server Manager。 导航到 Security > Settings 页面。 在Security for GIS Services选项卡, 确认Enable security for GIS Services 没有勾选。
5、 点击Save按钮禁用token。
在前端使用
1、ArcGIS Server 10.0的token需要同时通过cookie agstoken和参数token进行传值。如果不在参数中传递token,会报错:Unauthorized access。如果不在cookie agstoken中传递token,会报错:Invalid token。
2、示例代码:
fetch('/arcgis/tokens?request=getToken&username={你创建的用户名}&password={你创建的密码}&clientid=ip.*&expiration=60')
.then(response => {
response.text().then(token => {
token = token.trim()
document.cookie = `agstoken=${token}`
fetch(`/arcgis/rest/services/HAPipe/MapServer/?token=${token}&f=json`)
.then(response => {
response.json().then(json => {
console.log(json)
})
})
.catch(error => {
console.error(error)
})
})
})
.catch(error => {
console.error(error)
})