本文介绍了同时运行带有 SSL 和未加密的 Spring Boot 应用程序(嵌入式 Tomcat)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法运行 Spring Boot 应用程序(可运行的战争),以便它侦听两个端口 - 一个使用 SSL,一个不使用 SSL.我使用的是嵌入式 Tomcat 8.

Is there a way to run a Spring Boot application (runnable war) so that it listens on two ports - one with SSL and one without SSL. I am using an embedded Tomcat 8.

SSL 当前正在使用

@Bean
@Profile('tls')
EmbeddedServletContainerCustomizer servletContainerCustomizer ()
throws Exception {

    new EmbeddedServletContainerCustomizer () {
        @Override
        public void customize (ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container
            tomcat.addConnectorCustomizers (
                    new TomcatConnectorCustomizer () {
                        @Override
                        public void customize (Connector connector) {
                            connector.setPort     (Integer.parseInt (retrieveRequiredSpringProperty ('ssl.connection.port')))
                            connector.setSecure   (true)
                            connector.setScheme   ('https')
                            connector.setProtocol (retrieveSpringPropertyOrSpecified ('ssl.connection.protocol', 'HTTP/1.1'))

                            Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler ()
                            proto.setSSLEnabled   (true)
                            proto.setKeystoreFile (retrieveRequiredSpringProperty ('ssl.protocol.keystore.file'))
                            proto.setKeystorePass (retrieveRequiredSpringProperty ('ssl.protocol.keystore.password'))
                            proto.setKeyPass      (retrieveSpringPropertyOrSpecified ('ssl.protocol.keystore.cert.password', null))
                            proto.setKeystoreType ('JKS')
                            proto.setKeyAlias     (retrieveRequiredSpringProperty ('ssl.protocol.keystore.cert.name'))
                            proto.setSslProtocol  ('TLS')
                            proto.setClientAuth   ('false')
                        }
                    }
            )
        }
    }
}

我尝试使用多个 TomcatConnectorCustomizer,但列出的最后一个似乎覆盖而不是添加.

I have tried using multiple TomcatConnectorCustomizers but the last one listed seems to overwrite rather than be additive.

一如既往地欢迎任何建议!

Any suggestions are welcome as always!

推荐答案

TomcatConnectorCustomizer 仅修改现有的 Connector(名称中的线索).要添加其他连接器,您只需要一个不同的 API(例如 这里),例如(从样本中复制):

A TomcatConnectorCustomizer only modifies the existing Connector (clue in the name). To add additional connectors you just need a different API (example here), e.g. (copied from the sample):

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addAdditionalTomcatConnectors(createConnector());
    return tomcat;
}

这篇关于同时运行带有 SSL 和未加密的 Spring Boot 应用程序(嵌入式 Tomcat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:49