diff --git a/IX. ‘How-to’ guides/64.7. Enable Multiple Connectors with Tomcat.md b/IX. ‘How-to’ guides/64.7. Enable Multiple Connectors with Tomcat.md index e69de29..b7357cd 100644 --- a/IX. ‘How-to’ guides/64.7. Enable Multiple Connectors with Tomcat.md +++ b/IX. ‘How-to’ guides/64.7. Enable Multiple Connectors with Tomcat.md @@ -0,0 +1,35 @@ +### 64.7. 启用Tomcat的多连接器(Multiple Connectors) + +你可以将一个`org.apache.catalina.connector.Connector`添加到`TomcatEmbeddedServletContainerFactory`,这就能够允许多连接器,比如HTTP和HTTPS连接器: +```java +@Bean +public EmbeddedServletContainerFactory servletContainer() { + TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); + tomcat.addAdditionalTomcatConnectors(createSslConnector()); + return tomcat; +} + +private Connector createSslConnector() { + Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); + Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); + try { + File keystore = new ClassPathResource("keystore").getFile(); + File truststore = new ClassPathResource("keystore").getFile(); + connector.setScheme("https"); + connector.setSecure(true); + connector.setPort(8443); + protocol.setSSLEnabled(true); + protocol.setKeystoreFile(keystore.getAbsolutePath()); + protocol.setKeystorePass("changeit"); + protocol.setTruststoreFile(truststore.getAbsolutePath()); + protocol.setTruststorePass("changeit"); + protocol.setKeyAlias("apitester"); + return connector; + } + catch (IOException ex) { + throw new IllegalStateException("can't access keystore: [" + "keystore" + + "] or truststore: [" + "keystore" + "]", ex); + } +} + +```