1. 新建 OpenSSL 配置文件 (openssl.cnf)
- [req]
- distinguished_name = req_distinguished_name
- x509_extensions = v3_req
- prompt = no
- [req_distinguished_name]
- CN = My Local Server
- [v3_req]
- keyUsage = digitalSignature, keyEncipherment
- extendedKeyUsage = serverAuth
- subjectAltName = @alt_names
- [alt_names]
- IP.1 = 192.168.0.111 #内网测试地址
- IP.2 = 127.0.0.1
- DNS.1 = localhost
复制代码 2. 生成证书
Windows环境下可以在Git Bash中生成证书,其自带openssl- # 删除旧证书(如果存在)
- rm server.crt server.key
- # 生成新私钥(2048位)
- openssl genrsa -out server.key 2048
- # 生成证书签名请求(CSR)时应用修复配置
- openssl req -new -key server.key -out server.csr -config openssl.cnf
- # 生成证书时启用扩展(关键!)
- openssl x509 -req -days 365 -in server.csr \
- -signkey server.key -out server.crt \
- -extfile openssl.cnf -extensions v3_req # 显式应用扩展
复制代码 3. 转换为 PKCS12 格式
- openssl pkcs12 -export \
- -in server.crt -inkey server.key \
- -out keystore.p12 -name tomcat \
- -passout pass:changeit
复制代码 4. 拷贝证书文件
将生成的keystore.p12文件,拷贝到Tomcat的conf目录
5. 更新 Tomcat 配置 (conf/server.xml)
- <Connector
- port="8443"
- protocol="org.apache.coyote.http11.Http11NioProtocol"
- SSLEnabled="true"
- maxThreads="150">
- <SSLHostConfig protocols="TLSv1.3"
- certificateVerification="none"
- ciphers="TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384">
- <Certificate
- certificateKeystoreFile="conf/keystore.p12"
- certificateKeystorePassword="changeit"
- certificateKeystoreType="PKCS12"
- type="RSA"
- />
- </SSLHostConfig>
- </Connector>
复制代码 6. 重启 Tomcat 并测试
- 重启 Tomcat:
- # Linux
- ./bin/shutdown.sh && ./bin/startup.sh
- # Windows
- bin\shutdown.bat && bin\startup.bat
复制代码 - 访问测试:
浏览器打开:
- 浏览器会提示不安全(这是自签名证书的预期行为)。
- 点击 "高级" → "继续访问" 即可。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |