配置 API7 企业版与上游服务的 mTLS
mTLS 是一种双向的 TLS 认证方式,客户端和服务器相互验证身份。通常在高安全环境中实施,用于防止未经授权的访问并加强安全性。
本指南将演示如何配置 API7 网关与上游服务(以 NGINX 为例)之间的 mTLS。
前提条件
- 安装 API7 企业版。
- 在 API7 企业版上创建令牌。
生成证书和密钥
-
生成证书颁发机构 (CA) 的密钥和证书。
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 36500 -out ca.crt \
-subj "/CN=ROOTCA" -
为 API7 企业版生成服务器密钥和证书,通用名设置为
test.com,并使用 CA 证书签名。openssl genrsa -out server.key 2048 && \
openssl req -new -key server.key -out server.csr -subj "/CN=test.com" && \
cat > server.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = test.com
EOF
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out server.crt -days 36500 \
-sha256 -extfile server.ext -
为客户端生成密钥和证书,通用名设置为
CLIENT,并使用 CA 证书签名。openssl genrsa -out client.key 2048 && \
openssl req -new -key client.key -out client.csr -subj "/CN=client" && \
cat > client.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = clientAuth
EOF
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 36500 -sha256 -extfile client.ext -
生成证书和密钥后,检查本地设备上的文件:
❶
client.crt:客户端证书❷
client.key:客户端证书密钥❸
ca.crt:CA 证书