为 Spring 引导后端使用通配符 ssl 证书

Using wildcard ssl certificate for Spring Boot backend

我已经从我的托管商 (Hoster X) 那里为我的域获取了通配符 SSL 证书,例如domain.com.

现在我在服务器 A 上有一个 Spring 引导后端 运行(不托管在 Hoster X,IP:1.1.1.1,URL:api。domain.com) 和我的前端 运行 在另一台服务器 B 上(托管在 Hoster X,IP:2.2.2.2,URL app.domain.com).

api.domain.com 到 IP 1.1.1.1 的重定向是通过 Hoster X 的 DNS 配置中的 A 资源记录设置的,因为 domain.com 已在此处注册。

在我的 Spring 引导后端上安装证书的一般方法是什么,以便我的前端和后端之间的通信是安全的?我是否必须下载通配符证书才能将其安装到后端密钥库中?据我所知,我只能在托管在 Hoster X 服务器上的域的托管菜单中配置 SSL 证书。

在后端服务器上,使用openssl和以下命令将私钥文件、证书文件和CA文件合并到一个pkcs12密钥库中:

openssl pkcs12 -export -in <certfile> -inkey <keyfile> -out <keystorefile> -name tomcat -CAfile <cacertfile> -caname root

系统会提示您输入密钥库密码。现在将生成的密钥库文件(扩展名 'p12')复制到 Spring 启动应用程序的资源目录。

现在在 application.yml 中设置 Spring 引导 SSL 配置:

server:
    port: 8443

# Spring SSL configuration
    ssl:
        enabled: true
        key-store: classpath:keystore.p12
        key-store-password: "<KeystorePassword>"
        keyStoreType: PKCS12
        keyAlias: tomcat

或者,将 keystore.p12 复制到后端服务器的某处,并指向具有绝对路径的位置:

server:
    ssl:
        key-store: /path/to/keystore.p12

在前端机器的 DNS 设置中创建一个 A 记录很重要,这样所有前端对后端的请求都会通过对域的调用而不是 IP 进行,因为通配符证书仅对域名。