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

Spring Boot作为客户端启用HTTPS并使用WINDOWS-ROOT信任存储的问题

Spring Boot 客户端适配 WINDOWS-ROOT 信任存储类型解决方案

问题背景

当Spring Boot应用作为HTTPS服务器时,以下配置可正常运行:

server.ssl.enabled=true
server.ssl.key-store=. 
server.ssl.key-store-type=WINDOWS-MY
server.ssl.client-auth=need 

但添加server.ssl.trust-store相关配置后无法正常运行,且需要让应用作为客户端调用其他HTTPS服务时,使用WINDOWS-ROOT作为信任存储。

核心问题说明

server.ssl.*前缀的配置仅作用于Spring Boot作为HTTPS服务器的场景:

  • server.ssl.trust-store是用来配置服务器验证客户端证书时的信任存储,并非客户端调用外部服务时使用的信任存储。
  • 客户端调用外部HTTPS服务的SSL配置需要单独自定义,不能复用服务器端的配置项。

解决方案步骤

1. 保留服务器端原有正常配置

继续使用之前的服务器HTTPS配置,无需修改:

server.port=11443
server.ssl.enabled=true
server.ssl.key-store=. 
server.ssl.key-store-type=WINDOWS-MY
server.ssl.client-auth=need

2. 自定义客户端SSL上下文(以RestTemplate为例)

如果使用RestTemplate调用外部服务,需自定义实例,加载WINDOWS-ROOT作为信任存储:

2.1 添加必要依赖(若未包含)

确保项目中包含Apache HttpClient依赖(用于自定义SSL配置):

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2 编写RestTemplate配置类

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate windowsRootTrustRestTemplate() throws Exception {
        // 加载Windows根证书存储(WINDOWS-ROOT)
        KeyStore trustStore = KeyStore.getInstance("WINDOWS-ROOT");
        trustStore.load(null, null); // 无需密码,系统自动访问Windows证书存储

        // 构建SSL上下文,使用WINDOWS-ROOT作为信任存储
        SSLContextBuilder sslContextBuilder = SSLContextBuilder.create()
                .loadTrustMaterial(trustStore, null);

        // 创建SSL连接工厂
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());

        // 构建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
                        .setSSLSocketFactory(sslSocketFactory)
                        .build())
                .build();

        // 包装为RestTemplate
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        return new RestTemplate(requestFactory);
    }
}

3. 使用自定义RestTemplate调用外部服务

在业务代码中注入上述自定义的RestTemplate,即可用WINDOWS-ROOT验证外部服务的证书:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ExternalApiService {

    private final RestTemplate windowsRootTrustRestTemplate;

    @Autowired
    public ExternalApiService(RestTemplate windowsRootTrustRestTemplate) {
        this.windowsRootTrustRestTemplate = windowsRootTrustRestTemplate;
    }

    public String callExternalHttpsApi(String url) {
        return windowsRootTrustRestTemplate.getForObject(url, String.class);
    }
}

注意事项

  • 该配置仅在Windows环境下生效,因为WINDOWS-ROOT是Windows特有的证书存储类型。
  • 运行应用的用户需要有访问Windows证书存储的权限(通常普通用户权限即可)。
  • 如果使用其他HTTP客户端(如WebClient),需对应调整SSL上下文的配置逻辑,核心都是加载WINDOWS-ROOT作为信任存储。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 11:41:09