通过HTTPS访问OGC服务

作者

杰夫麦克纳

联系

jmckenna在gatewaygeomatics.com

原作者

诺曼德·萨瓦德

最后更新

2021-04-13

介绍

以下文档解释了如何将MapServer设置为客户端,以便使用HTTPS协议通过安全的SSL连接访问WMS/WFS服务器。它描述了用户可能遇到的常见问题以及如何解决这些问题。

要求

MapServer 5.4.1或更高版本,使用 cURL 。CURL必须使用SSL支持构建。您还应执行配置相关OGC客户端支持的完整步骤,具体步骤如下:

验证安装

CURL可执行文件

首先检查cURL是否在本地找到,并且是使用SSL支持构建的:

curl --version

在Linux系统上,响应应该类似于(请注意 ssl 协议):

curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1g zlib/1.2.8
Release-Date: 2020-01-08
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS HTTPS-proxy Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSockets

在Windows上,响应应类似于(请注意 ssl 协议):

curl 7.68.0 (i386-pc-win32) libcurl/7.68.0 OpenSSL/1.1.1d zlib/1.2.7 WinIDN
Release-Date: 2020-01-08
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI UnixSockets libz

小技巧

MS4W用户可以先执行cURL setenv.bat

接下来,使用curl命令行验证您与远程HTTPS服务的连接,例如:

curl https://demo.mapserver.org/cgi-bin/wms?

您应该会收到如下结果:

No query information to decode. QUERY_STRING is set, but empty.

如果收到如下错误消息,则可能是您没有正确指向本地CA捆绑包:

curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

CA捆绑

CURL CA(证书颁发机构)捆绑包文件必须位于本地计算机/服务器上。

下载http://curl.haxx.se/docs/caextract.html上的CA捆绑包文件“cacert.pem”,如果您有cURL源代码,则可以通过执行“make ca-Bundle”或“make ca-Firefox”(如果您安装了Firefox和Certutil工具)来创建CA捆绑包。如果您使用第二种选择,则包文件将被命名为ca-bundle.crt,并将在curl根目录下的lib目录中找到。有关更多详细信息,请参阅http://curl.haxx.se/docs/caextract.html。将此文件存储在 CURL_CA_BUNDLE 环境变量。

设置 CURL_CA_BUNDLE 指向捆绑包文件的环境变量(例如,将cURL_CA_Bundle=/Path/导出到/my-ca-bundle.ext,其中my-ca-bundle.ext可以是cacert.pem或ca-bundle.crt)。

备注

MS4W带有已经为Windows用户配置的HTTPS,其配置在https://ms4w.com/README_INSTALL.html#certificates-for-https上有解释

映射文件设置

编辑 Mapfile 以添加WMS连接URL。例如:

CONNECTION     "https://demo.mapserver.org/cgi-bin/wms?"
CONNECTIONTYPE WMS

使用 shp2img 实用程序,以验证MapServer是否可以通过HTTPS连接并生成有效的地图图像,例如命令:

shp2img -m test.map -o ttt.png -map_debug 3

如果从MapServer收到类似以下内容的错误,则表示您没有正确指向本地CA包:

msDrawMap(): WMS connection error. Failed to draw WMS layer named 'country_bounds'. This most likely happened because the remote WMS server returned an invalid image, and XML exception or another unexpected result in response to the GetMap request. Also check and make sure that the layer's connection URL is valid. <br>
msDrawWMSLayerLow(): WMS server error. WMS GetMap request failed for layer 'country_bounds' (Status -60: SSL certificate problem: unable to get local issuer certificate). <br>
msHTTPExecuteRequests(): HTTP request error. HTTP: request failed with curl error code 60 (SSL certificate problem: unable to get local issuer certificate) for https://demo.mapserver.org/cgi-bin/wms?LAYERS=country_bounds&REQUEST=GetMap&SERVICE=WMS&FORMAT=image/gif&STYLES=&HEIGHT=300&VERSION=1.1.1&SRS=EPSG:4326&WIDTH=400&BBOX=-180.451127819549,-135.338345864662,180.451127819549,135.338345864662&TRANSPARENT=TRUE <br>

具有自签名SSL证书的远程服务器

如果出现以下错误,则意味着您的远程服务器可能使用自签名的SSL证书,并且服务器证书不包含在您的CA包文件中。

curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.

要获取远程服务器证书,必须执行以下命令:

openssl s_client -connect domainname:port

将所有内容从“--begin certificate-----”标记复制到“--end certificate-----”标记。将其粘贴到my-ca-bundle.ext文件的末尾。

如果您收到错误“验证错误:无法获取本地颁发者证书”,则可能缺少中间证书。如果您本地保存了证书文件,则可以将此文件作为参数提供给 OpenSSL 具体如下:

openssl s_client -CAfile C:\Certs\curl-ca-bundle.crt -connect domainname:port

使用cURL命令行验证您的连接:

curl https://demo.mapserver.org/cgi-bin/wms?

备注

如果出现以下错误,则表示URL请求中的域名与创建远程服务器证书时声明的域名不对应。

curl: (51) SSL: certificate subject name 'domainname' does not match target host name 'domainname'

您必须使用与生成远程服务器证书时使用的“公用名”提示中显示的域名完全相同的域名。例如,不能使用远程服务器IP。这意味着以下URL是不可接受的。

CONNECTION "https://xxx.xxx.xxx.xxx:port/cgi-bin/mapserv?map=/path/to/wms.map"
CONNECTIONTYPE WMS

备注

强烈建议您查看 MAP= 通过设置来调用MapServer可执行文件 MS_MAP_PATTERNMS_MAP_NO_PATH 或者隐藏 MAP= 参数,如本文档中所建议的 限制 Mapfile 访问 。中列出了保护服务器安全的所有可能环境变量 环境变量