0

我正在使用带有 Spring 的嵌入式 Tomcat,并以编程方式将其配置为侦听端口 8443 以获取 https 并且它可以工作。下面是我的 Apllication.java 文件中的代码片段。如何将其配置为也侦听端口 80 的 http 请求?

@Bean
    EmbeddedServletContainerCustomizer containerCustomizer(
            @Value("${keystore.file:src/main/resources/private/keystore}") String keystoreFile,
            @Value("${keystore.pass:changeit}") final String keystorePass) throws Exception {


        final String absoluteKeystoreFile = new File(keystoreFile).getAbsolutePath();

        return new EmbeddedServletContainerCustomizer () {

            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                    TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;



                    tomcat.addConnectorCustomizers(
                            new TomcatConnectorCustomizer() {
                                @Override
                                public void customize(Connector connector) {
                                    connector.setPort(8443);
                                    connector.setSecure(true);
                                    connector.setScheme("https");

                                    Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler();
                                    proto.setSSLEnabled(true);
                                    proto.setKeystoreFile(absoluteKeystoreFile);
                                    proto.setKeystorePass(keystorePass);
                                    proto.setKeystoreType("JKS");
                                    proto.setKeyAlias("tomcat");

                                }
                            });



            }
        };
    }
4

1 回答 1

1

我找到了解决方案。在 customize() 方法中,我使用了 tomcat.addAdditionalTomcatConnectors(httpConnector()) 方法来添加新的 http 端口

  // create an http port for downloading images over http
    private Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(8383);
            return connector;

    }
于 2014-11-18T16:32:13.537 回答