meface/docs/article/gis/geoserver/authkeyplugin.md

120 lines
4.0 KiB
Markdown
Raw Normal View History

2024-01-16 11:21:05 +08:00
---
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) 模块
![image-20231218104724991](./images/image-20231218104724991.png)
解压到 `webapps\geoserver\WEB-INF\lib` 目录下,重启应用。
### 3.插件配置
#### 3.1 添加认证`user property`
新增验证过滤器,这里有两种方式`user property``web service`;我们先选这第一种:
> 第一种是在GeoServer 内部进行authkey的验证。
>
> 第二种是通过调用外部接口,在外部验证,正确后返回一个用户名的方式进行鉴权。
<img src="./images/image-20231218105240848.png" alt="image-20231218105240848" style="zoom:50%;display:block;width:50%;" /><img src="images/image-20231218110226274.png" alt="image-20231218110226274" style="zoom:80%;display:block;width:50%" />
点击`Synchronize user/group service` 将会生成对应用户/组服务中用户的UUID的key。
<img src="images/image-20231218110655977.png" alt="image-20231218110655977" style="zoom:80%;" />
#### 3.2 添加过滤器链
![image-20231218111311090](images/image-20231218111311090.png)
<img src="images/image-20231218111238452.png" alt="image-20231218111238452" style="zoom:80%;" />
将新增的`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`
![image-20231218112321622](./images/image-20231218112321622.png)
返回的是401`HTTP ERROR 401 Unauthorized`。添加`authKey`参数后才可以正常访问:
![image-20231218112541774](./images/image-20231218112541774.png)
#### 3.4 添加认证`web service`
![image-20231219134339225](./images/image-20231219134339225.png)
在后台实现一个接口接受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 添加过滤器链
![image-20231219134702440](./images/image-20231219134702440.png)
只保留刚刚新建的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`,没有权限。
添加后可以正常访问服务:
![image-20231219135006360](./images/image-20231219135006360.png)
### 4.总结
AuthKey插件可以让访问GeoServer的请求都添加一步认证的过程只有认证通过的请求才能正常返回。
这种方式可以理解成在GeoServer服务端与客户端之间加了一堵墙而已而进一步的细化的服务安全服务操作和图层数据安全数据访问规则需要在服务端另外进行配置。
### 参考文章
[1] [密钥验证模块 — GeoServer 2.24.x User Manual (osgeo.cn)](https://www.osgeo.cn/geoserver-user-manual/extensions/authkey/index.html)