spring 如何在不使用文件的情况下将Java KeyStore证书加载到Tomcat服务器

mlmc2os5  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(131)

我已经从Azure Key Vault导入了一个证书,并将其加载到Java KeyStore对象中。我应该以编程方式将此对象导入到Tomcat服务器中,即不使用文件。这是否可行,如果可行,如何操作?
来自Azure密钥库的证书已成功加载到Java KeyStore对象中。但是,我不熟悉如何通过Java代码配置Tomcat。我尝试使用ChatGPT作为起点,它建议如下:

  1. import org.apache.catalina.Context;
  2. import org.apache.catalina.connector.Connector;
  3. import org.apache.coyote.http11.Http11NioProtocol;
  4. import org.apache.tomcat.util.net.SSLHostConfig;
  5. import org.apache.tomcat.util.net.SSLHostConfigCertificate;
  6. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  7. import org.springframework.boot.web.server.ConfigurableWebServerFactory;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import java.security.KeyStore;
  11. @Configuration
  12. public class TomcatSslConfiguration {
  13. @Bean
  14. public ConfigurableWebServerFactory webServerFactory() {
  15. TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
  16. @Override
  17. protected void postProcessContext(Context context) {
  18. // Programmatically configure SSL
  19. configureSsl(context);
  20. }
  21. };
  22. return factory;
  23. }
  24. private void configureSsl(Context context) {
  25. // Create and load your KeyStore programmatically
  26. KeyStore keyStore = loadKeyStore();
  27. // Create an SSLHostConfigCertificate
  28. SSLHostConfigCertificate sslHostConfigCertificate = new SSLHostConfigCertificate();
  29. sslHostConfigCertificate.setCertificateKeystore(keyStore);
  30. // Create an SSLHostConfig
  31. SSLHostConfig sslHostConfig = new SSLHostConfig();
  32. sslHostConfig.addCertificate(sslHostConfigCertificate);
  33. // Set the SSLHostConfig in the Tomcat context
  34. context.addSslHostConfig(sslHostConfig);
  35. }
  36. private KeyStore loadKeyStore() {
  37. // Implement your custom logic to load the KeyStore
  38. // For example, you can use KeyStore.getInstance("JKS") and load it from a custom source
  39. // KeyStore keyStore = ...
  40. return keyStore;
  41. }
  42. }

字符串
无论如何,似乎context.addSslHostConfig不存在。我不确定这是否是一个好的起点。感谢您的任何建议。

carvr3hs

carvr3hs1#

我过去也尝试过使用自定义的ssl配置,我不想指定路径和密码作为属性,而只是传递一个keystore,或keymanager/trustmanager,甚至已经初始化的sslcontext。但是这是不可能的。请参阅我的问题,关于类似于你的问题:Configuring SSL programatically of a Spring Boot server with Tomcat is failing
我在Tomcat的GitHub上打开了一个pull request,看这里:https://github.com/apache/tomcat/pull/673看看PR上的评论,似乎他们正在内部讨论是否启用它。

相关问题