使用 SpringBoot SslBundles 和 RestTemplate 的mutualTLS

问题描述 投票:0回答:1

我有一个 SpringBoot 应用程序(客户端),它对外部服务(服务器)进行 REST 调用。客户端和服务器之间的通信应通过相互 TLS 进行保护。因此,在此设置中,我们需要密钥库中的客户端证书和信任库中的服务器 CA 证书。

当我在Postman中导入这个证书材料时,我可以成功提出请求。含义:证书有效。

尽管如此,到目前为止,我还无法使其在 SpringBoot 中运行。我想利用 SpringBoot 3.1 引入的 SslBundles

我定义了一个包含密钥库和信任库的捆绑包:

spring:
  ssl:
    bundle:
      jks:
        mySslBundle:
          keystore:
            location: "classpath:path/to/clientCert.p12"
            password: "PW"
            type: "PKCS12"
          truststore:
            location: "classpath:path/to/serverCA.p12"
            password: "PW"
            type: "PKCS12"

然后我可以将此捆绑包绑定到 RestTemplate:

@Configuration
public class RestTemplateConfig {

  @Bean(name = "fooRestTemplate")
  public RestTemplate createFooRestTemplate(RestTemplateBuilder restTemplateBuilder,
      SslBundles sslBundles) {
    return restTemplateBuilder
        .setSslBundle(sslBundles.getBundle("mySslBundle"))
        .build();
  }

}

当客户端在应用程序内向服务器发送请求时,我收到以下错误:

Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

在 Postman 中,我必须将服务器证书加载为 .pem 文件。 在我的 SpringBoot 应用程序中,我使用 openssl 将 .pem 文件转换为 .p12。我这样做是为了客户端证书和服务器证书都是 p12 文件,并且可以导入到同一个 sslBunde 中。这是在同一包中导入密钥库和 trustore 的正确方法吗?

任何线索表示赞赏。

java spring-boot ssl resttemplate mutual-authentication
1个回答
0
投票

正如评论中所建议的,使用 keytool 而不是 openssl 将 .crt 服务器证书转换为 .p12 解决了我的问题。

所以,是的,像我一样将 keystore 和 trustore 打包在同一个包中是可以的,如果需要转换来生成 .p12 文件,我们应该使用 keytool。

© www.soinside.com 2019 - 2024. All rights reserved.