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

mlmc2os5  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(42)

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

import org.apache.catalina.Context;
    import org.apache.catalina.connector.Connector;
    import org.apache.coyote.http11.Http11NioProtocol;
    import org.apache.tomcat.util.net.SSLHostConfig;
    import org.apache.tomcat.util.net.SSLHostConfigCertificate;
    import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    import org.springframework.boot.web.server.ConfigurableWebServerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.security.KeyStore;
    
    @Configuration
    public class TomcatSslConfiguration {
    
        @Bean
        public ConfigurableWebServerFactory webServerFactory() {
            TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
                @Override
                protected void postProcessContext(Context context) {
                    // Programmatically configure SSL
                    configureSsl(context);
                }
            };
            return factory;
        }
    
        private void configureSsl(Context context) {
            // Create and load your KeyStore programmatically
            KeyStore keyStore = loadKeyStore();
    
            // Create an SSLHostConfigCertificate
            SSLHostConfigCertificate sslHostConfigCertificate = new SSLHostConfigCertificate();
            sslHostConfigCertificate.setCertificateKeystore(keyStore);
    
            // Create an SSLHostConfig
            SSLHostConfig sslHostConfig = new SSLHostConfig();
            sslHostConfig.addCertificate(sslHostConfigCertificate);
    
            // Set the SSLHostConfig in the Tomcat context
            context.addSslHostConfig(sslHostConfig);
        }
    
        private KeyStore loadKeyStore() {
            // Implement your custom logic to load the KeyStore
            // For example, you can use KeyStore.getInstance("JKS") and load it from a custom     source
            // KeyStore keyStore = ...
    
            return keyStore;
        }
    }

字符串
无论如何,似乎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上的评论,似乎他们正在内部讨论是否启用它。

相关问题