---
title: GeoServer安全-AuthKey
author: ac
date: 2013-12-17
categries:
- GIS
tags:
- AuthKey
---
## GeoServer安全-AuthKey
### 1.简述
`authkey` 鉴权可以在GeoServer中实现像调用天地图、高德地图那样,通过提供URL中的Key进行鉴权,如:
```text
http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.3.0&request=GetCapabilities&authkey=ef18d7e7-963b-470f-9230-c7f9de166888
```
其中的`authkey`与特定用户相关联,作为唯一身份验证令牌。
### 2. 插件安装
下载[Key authentication](https://sourceforge.net/projects/geoserver/files/GeoServer/2.22.1/extensions/geoserver-2.22.1-authkey-plugin.zip) 模块

解压到 `webapps\geoserver\WEB-INF\lib` 目录下,重启应用。
### 3.插件配置
#### 3.1 添加认证`user property`
新增验证过滤器,这里有两种方式`user property`,`web service`;我们先选这第一种:
> 第一种:是在GeoServer 内部进行authkey的验证。
>
> 第二种是通过调用外部接口,在外部验证,正确后返回一个用户名的方式进行鉴权。

点击`Synchronize user/group service` 将会生成对应用户/组服务中用户的UUID的key。
#### 3.2 添加过滤器链

将新增的`prop_auth`认证添加到选择的框中,去除匿名的认证`anonymous`,关闭,保存。
#### 3.3 测试
`http://localhost:8080/geoserver/nurc/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fjpeg&TRANSPARENT=true&STYLES&LAYERS=nurc%3AArc_Sample&exceptions=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&WIDTH=768&HEIGHT=384&BBOX=-293.12500078125%2C-127.18750078125001%2C246.71875078124998%2C142.65625078125`

返回的是401,`HTTP ERROR 401 Unauthorized`。添加`authKey`参数后才可以正常访问:

#### 3.4 添加认证`web service`

在后台实现一个接口,接受authkey参数。
```java
@GetMapping(value="/checkAuthKey")
public String checkAuthKey(@RequestParam String authkey){
try{
String username = sysUserService.checkAuthKey(authkey);
if(StringUtils.isBlank(username)){
System.out.println("没有关联的username");
}
return username;
}catch (Exception e){
}
return "pg_admin";
}
```
#### 3.5 添加过滤器链

只保留刚刚新建的service_auth认证。
#### 3.6 验证
`http://localhost:8080/geoserver/tiger/wms?service=WMS&version=1.1.0&request=GetMap&layers=tiger:poi&bbox=-74.0118315772888,40.70754683896324,-74.00153046439813,40.719885123828675&width=641&height=768&srs=EPSG:4326&styles=&format=image/jpeg`
没有添加`authKey`参数,后台响应式`HTTP ERROR 403 Access Denied`,没有权限。
添加后可以正常访问服务:

### 4.总结
AuthKey插件可以让访问GeoServer的请求都添加一步认证的过程,只有认证通过的请求才能正常返回。
这种方式可以理解成在GeoServer服务端与客户端之间加了一堵墙而已,而进一步的细化的服务安全(服务操作)和图层数据安全(数据访问规则)需要在服务端另外进行配置。
### 参考文章
[1] [密钥验证模块 — GeoServer 2.24.x User Manual (osgeo.cn)](https://www.osgeo.cn/geoserver-user-manual/extensions/authkey/index.html)