You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.4+Netty HTTP/3控制器连接失败,疑证书配置有误求助

解决Spring Reactor HTTP/3连接拒绝问题(证书+配置修正)

一、正确的自签名证书生成与信任配置

你的证书生成命令缺少SAN(Subject Alternative Name)字段,且未完成系统信任配置,这会导致Chrome拒绝建立QUIC连接:

1. 生成带SAN字段的证书

keytool -genkeypair -alias http3 -keyalg RSA -keysize 4096 \
  -validity 3650 -dname "CN=localhost" -keypass changeit -keystore keystore.p12 \
  -storeType PKCS12 -storepass changeit -ext SAN=DNS:localhost,IP:127.0.0.1

添加-ext SAN是为了满足现代浏览器对证书身份验证的要求,避免因缺少SAN字段触发的安全拦截

2. 导出公钥并导入系统信任库

keytool -exportcert -alias http3 -keystore keystore.p12 -storepass changeit -file localhost.crt -rfc
  • Windows:双击localhost.crt → 安装证书 → 选择「本地计算机」→ 「受信任的根证书颁发机构」→ 完成导入
  • macOS:双击localhost.crt → 钥匙串访问中找到证书 → 右键「显示简介」→ 信任→ 「使用此证书时」设为「始终信任」
  • Linux:将localhost.crt复制到/usr/local/share/ca-certificates/,执行sudo update-ca-certificates

二、Spring应用配置修正

HTTP/3基于QUIC协议使用UDP端口,你当前仅配置了TCP端口,需补充UDP端口与HTTP/3启用配置:

1. application.properties调整

spring.application.name=http3app
# TCP端口(兼容HTTP/1.1/HTTP/2)
server.port=8443
# HTTP/3 QUIC UDP端口(可与TCP端口复用)
server.http3.port=8443
spring.ssl.bundle.jks.http3.key.alias=http3
spring.ssl.bundle.jks.http3.keystore.location=classpath:keystore.p12
spring.ssl.bundle.jks.http3.keystore.password=changeit
spring.ssl.bundle.jks.http3.keystore.type=PKCS12
# 强制启用HTTP/3
server.http3.enabled=true

2. 修正WebServerCustomizer代码

当前配置仅监听HTTP/3协议,导致TCP连接请求被拒绝,建议同时兼容多协议:

@Component
class Http3NettyWebServerCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers(server -> {
            SslBundle sslBundle = factory.getSslBundles().getBundle("http3");
            Http3SslContextSpec sslContextSpec =
                    Http3SslContextSpec.forServer(sslBundle.getManagers().getKeyManagerFactory(), sslBundle.getKey().getPassword());

            return server
                    // 同时支持HTTP/1.1、HTTP/2、HTTP/3
                    .protocol(HttpProtocol.H1, HttpProtocol.H2, HttpProtocol.HTTP3)
                    .secure(spec -> spec.sslContext(sslContextSpec))
                    .http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
                            .maxData(10_000_000)
                            .maxStreamDataBidirectionalRemote(1_000_000)
                            .maxStreamsBidirectional(100));
        });
    }
}

三、Chrome HTTP/3支持确认

  1. 地址栏输入chrome://flags/#enable-quic
  2. 将「Experimental QUIC protocol」设为「Enabled」
  3. 重启Chrome

四、验证步骤

  1. 启动应用,确认日志输出包含「Netty started on port 8443 (HTTP/3)」
  2. 访问https://localhost:8443/hello(HTTP/3必须基于HTTPS)
  3. 打开开发者工具→网络→查看请求的「协议」字段,显示h3即为成功

内容的提问来源于stack exchange,提问作者ThinkDifferent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 00:39:52