70.9 启用Tomcat的多连接器

你可以将org.apache.catalina.connector.Connector添加到TomcatEmbeddedServletContainerFactory,这就能够允许多连接器,比如HTTP和HTTPS连接器:

  1. @Bean
  2. public EmbeddedServletContainerFactory servletContainer() {
  3. TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
  4. tomcat.addAdditionalTomcatConnectors(createSslConnector());
  5. return tomcat;
  6. }
  7. private Connector createSslConnector() {
  8. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  9. Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
  10. try {
  11. File keystore = new ClassPathResource("keystore").getFile();
  12. File truststore = new ClassPathResource("keystore").getFile();
  13. connector.setScheme("https");
  14. connector.setSecure(true);
  15. connector.setPort(8443);
  16. protocol.setSSLEnabled(true);
  17. protocol.setKeystoreFile(keystore.getAbsolutePath());
  18. protocol.setKeystorePass("changeit");
  19. protocol.setTruststoreFile(truststore.getAbsolutePath());
  20. protocol.setTruststorePass("changeit");
  21. protocol.setKeyAlias("apitester");
  22. return connector;
  23. }
  24. catch (IOException ex) {
  25. throw new IllegalStateException("can't access keystore: [" + "keystore"
  26. + "] or truststore: [" + "keystore" + "]", ex);
  27. }
  28. }