使用SpringBoot SslBundles和RestTemplate的mutualTLS

gz5pxeao  于 2024-01-08  发布在  Spring
关注(0)|答案(1)|浏览(257)

我有一个SpringBoot应用程序(客户端),它对外部服务(服务器)进行REST调用。客户端和服务器之间的通信应该使用双向TLS进行保护。因此,在此设置中,我们需要在密钥库中提供客户端证书,并在信任库中为服务器提供CA证书。
当我在Postman中导入此证书材料时,我可以成功地进行请求。含义:证书有效。
尽管如此,到目前为止,我还不能在SpringBoot中使用它。我想利用SpringBoot 3.1引入的SslBundles
我定义了一个包含keystore和truststore的bundle:

  1. spring:
  2. ssl:
  3. bundle:
  4. jks:
  5. mySslBundle:
  6. keystore:
  7. location: "classpath:path/to/clientCert.p12"
  8. password: "PW"
  9. type: "PKCS12"
  10. truststore:
  11. location: "classpath:path/to/serverCA.p12"
  12. password: "PW"
  13. type: "PKCS12"

字符串
然后我可以将这个bundle绑定到RestTemplate:

  1. @Configuration
  2. public class RestTemplateConfig {
  3. @Bean(name = "fooRestTemplate")
  4. public RestTemplate createFooRestTemplate(RestTemplateBuilder restTemplateBuilder,
  5. SslBundles sslBundles) {
  6. return restTemplateBuilder
  7. .setSslBundle(sslBundles.getBundle("mySslBundle"))
  8. .build();
  9. }
  10. }


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

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


在Postman中,我必须将服务器证书加载为.pem文件。在我的SpringBoot应用中,我使用openssl将.pem文件转换为.p12。我这样做是为了使客户端证书和服务器证书都是p12文件,并且可以导入到同一个sslBunde中。这是在同一个bundle中导入keystore和trustore的正确方法吗?
任何线索都很感激。

juzqafwq

juzqafwq1#

正如在评论中所建议的那样,使用keytool而不是openssl将.crt服务器证书转换为.p12解决了我的问题。
所以,是的,可以像我一样将keystore和trustore打包在同一个包中,如果需要转换以生成.p12文件,我们应该使用keytool。

相关问题