You need to enable JavaScript to run this app. Spring WebClient遇证书域名不匹配 如何配置HostnameVerifier绕过SSL校验
优惠活动
大模型
产品
解决方案
定价
更多

Spring WebClient遇证书域名不匹配 如何配置HostnameVerifier绕过SSL校验

Spring WebClient 调用SSL接口的域名匹配异常处理

异常与背景说明

  • 基于Spring WebClient发起带SSL的Web服务调用时,抛出 java.security.cert.CertificateException: No subject alternative DNS name matching lizzad.int.octa.com 异常。
  • 经排查确认,该报错逻辑自洽:服务端当前提供的证书未包含目标域名lizzad.int.octa.com的DNS Subject Alternative Name条目;同套逻辑调用其他服务URL时,请求可正常完成。
  • 业务场景:前期接到需求调整服务调用URL,对接方明确告知新旧URL使用同一张证书,但调整配置后服务始终运行失败;测试环境对接方提出临时需求,希望通过绕开部分校验逻辑保障测试流程跑通。

已尝试的配置及实际效果

  • 基于Reactor Netty的TcpClient自定义SSL配置,意图实现信任所有证书、绕过主机名校验的效果,实际编写的配置代码如下:
TcpClient tcpClient = TcpClient.create().secure(sslContextSpec -> {
    // 配置SSL参数
    SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
    sslContextBuilder
            .trustManager(InsecureTrustManagerFactory.INSTANCE);

    sslContextSpec.sslContext(sslContextBuilder)
            .defaultConfiguration(SslProvider.DefaultConfigurationType.NONE)
            .handshakeTimeoutMillis(30)
            .closeNotifyFlushTimeoutMillis(12000)
            .closeNotifyReadTimeoutMillis(12000)
            .handlerConfigurator(
                    (handler) -> {
                        SSLEngine engine = handler.engine();
                        engine.setNeedClientAuth(true);
                        SSLParameters params = new SSLParameters();
                        List<SNIMatcher> matchers = new LinkedList<>();
                        SNIMatcher matcher = new SNIMatcher(0) {
                            @Override
                            public boolean matches(SNIServerName serverName) {
                                return true;
                            }
                        };
                        matchers.add(matcher);
                        params.setSNIMatchers(matchers);
                        engine.setSSLParameters(params);
                    }
            );
});
HttpClient httpClient = HttpClient.from(tcpClient);

return WebClient.builder()
        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE)
        .clientConnector(new ReactorClientHttpConnector(httpClient)).build();
  • 实测验证:上述自定义SNIMatcher的配置逻辑无法实现跳过主机名校验的效果,目标异常仍持续抛出。
  • 参考通用HTTPS调用场景的处理方案:非WebClient栈下,可通过实现始终返回true的自定义HostnameVerifier对象绕过主机名校验,示例代码如下:
new javax.net.ssl.HostnameVerifier() {
    public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
        return true;
    }
};

核心待解问题

  • 当前使用Spring WebClient的技术栈下,是否支持实现自定义HostnameVerifier的配置逻辑
  • 可落地的具体配置方法是什么,能够解决调用时抛出的java.security.cert.CertificateException: No subject alternative DNS name matching lizzad.int.octa.com异常,满足测试环境临时对接的需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.29 12:24:03