温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

tomcat如何开启HTTPS

发布时间:2026-08-28 09:03:23 来源:亿速云 阅读:84 作者:小樊 栏目:系统运维

Apache Tomcat 中开启 HTTPS,核心思路是:
准备证书 → 配置 server.xml → 重启 Tomcat。下面分几种常见场景说明。


一、使用自签名证书(测试/本地开发)

1️⃣ 生成自签名证书(JDK keytool)

keytool -genkeypair \
  -alias tomcat \
  -keyalg RSA \
  -keysize 2048 \
  -storetype PKCS12 \
  -keystore keystore.p12 \
  -validity 365

过程中会提示:

  • 密码(记住,后面要用)
  • 名字与姓氏(建议填 localhost

生成文件:keystore.p12


2️⃣ 配置 Tomcat(server.xml)

编辑:

$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
✅ 证书路径可以用绝对路径


3️⃣ 重启 Tomcat

bin/shutdown.sh
bin/startup.sh

或 Windows:

bin/shutdown.bat
bin/startup.bat

4️⃣ 访问测试

https://localhost:8443

浏览器会提示“不安全”(自签名证书正常)


二、使用 CA 证书(正式环境)

方式一:使用 PFX / PKCS12 证书(云厂商常见)

假设你有:

server.pfx
<Connector port="443"
    protocol="org.apache.coyote.http11.Http11NioProtocol"
    SSLEnabled="true">

    <SSLHostConfig>
        <Certificate
            certificateKeystoreFile="/path/server.pfx"
            certificateKeystorePassword="证书密码"
            certificateKeystoreType="PKCS12" />
    </SSLHostConfig>
</Connector>

方式二:使用 Nginx + Tomcat(推荐生产环境)

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;
    }
}

更安全、更易维护


三、强制 HTTP 跳转 HTTPS(可选)

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>

四、常见问题排查

❌ Tomcat 启动失败

  • 端口被占用(443 / 8443)
  • 证书路径错误
  • 密码错误

查看日志:

logs/catalina.out

❌ HTTPS 访问没反应

  • 是否使用 https://
  • 是否端口正确
  • 防火墙是否放行端口

五、总结

场景 推荐方式
本地测试 自签名证书
正式上线 CA 证书
高并发生产 Nginx + Tomcat
安全 不直接暴露 Tomcat

如果你愿意,可以告诉我:

  • ✅ Tomcat 版本(8 / 9 / 10)
  • ✅ 使用的证书格式(p12 / pfx / jks / pem)
  • ✅ 操作系统(Linux / Windows)

我可以给你 完全对应你环境的配置示例

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI