在 Apache Tomcat 中开启 HTTPS,核心思路是:
准备证书 → 配置 server.xml → 重启 Tomcat。下面分几种常见场景说明。
keytool -genkeypair \
-alias tomcat \
-keyalg RSA \
-keysize 2048 \
-storetype PKCS12 \
-keystore keystore.p12 \
-validity 365
过程中会提示:
localhost)生成文件:keystore.p12
编辑:
$TOMCAT_HOME/conf/server.xml
找到(或新增) HTTPS Connector:
<Connector
port="8443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150"
SSLEnabled="true"
scheme="https"
secure="true">
<SSLHostConfig>
<Certificate
certificateKeystoreFile="conf/keystore.p12"
certificateKeystorePassword="你的密码"
certificateKeystoreType="PKCS12"
type="RSA" />
</SSLHostConfig>
</Connector>
✅ 推荐端口:
8443
✅ 证书路径可以用绝对路径
bin/shutdown.sh
bin/startup.sh
或 Windows:
bin/shutdown.bat
bin/startup.bat
https://localhost:8443
浏览器会提示“不安全”(自签名证书正常)
假设你有:
server.pfx
<Connector port="443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
SSLEnabled="true">
<SSLHostConfig>
<Certificate
certificateKeystoreFile="/path/server.pfx"
certificateKeystorePassword="证书密码"
certificateKeystoreType="PKCS12" />
</SSLHostConfig>
</Connector>
Tomcat 不处理 HTTPS,由 Nginx 处理:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
location / {
proxy_pass http://localhost:8080;
}
}
✅ 更安全、更易维护
在 web.xml 中:
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPS</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
查看日志:
logs/catalina.out
https://| 场景 | 推荐方式 |
|---|---|
| 本地测试 | 自签名证书 |
| 正式上线 | CA 证书 |
| 高并发生产 | Nginx + Tomcat |
| 安全 | 不直接暴露 Tomcat |
如果你愿意,可以告诉我:
我可以给你 完全对应你环境的配置示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。