title: Spring Boot 2 programmatic configuration of Embedded Servlet Container
body: <p>I'm attempting to configure the embedded Tomcat provided by Spring Boot 2, my main goal being to have multiple connectors based on various deployment models.</p>
  
  <p>For this I like to have the following setup in place:</p>
  
  <ul>
  <li>An HTTP connector redirecting over to an HTTPS one for one case</li>
  <li>An AJP connector handling requests from a fronting Apache server.</li>
  </ul>
  
  <p>So far regarding the second case I think I have been able to put the configuration in place to do so I created the following configuration class:</p>
  
  <pre><code>@Configuration
  public class EmbeddedWebServerConfiguration {
  
      @Value("${server.port}")
      private int httpPort;
  
      @Value("${server.secure.port}")
      private int httpsPort;
  
      @Value("${server.uses.ssl}")
      private boolean usesSSL;
  
      @Value("${server.uses.ajp}")
      private boolean usesAjp;
  
      @Value("${server.ajp.port}")
      private int ajpPort;
  
      @Bean
      public ServletWebServerFactory servletContainer() {
      TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  
          @Override
          protected void postProcessContext(Context context) {
          SecurityConstraint securityConstraint = new SecurityConstraint();
          securityConstraint.setUserConstraint("CONFIDENTIAL");
          SecurityCollection securityCollection = new SecurityCollection();
          securityCollection.addPattern("/*");
          securityConstraint.addCollection(securityCollection);
          context.addConstraint(securityConstraint);
          }
  
      };
  
      if (usesSSL) {
          tomcat.addAdditionalTomcatConnectors(redirectConnector());
      }
  
      if (usesAjp) {
          tomcat.addAdditionalTomcatConnectors(ajpConnector());
      }
  
      return tomcat;
      }
  
      private Connector redirectConnector() {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      connector.setScheme("http");
      connector.setPort(httpPort);
      connector.setSecure(false);
      connector.setRedirectPort(httpsPort);
  
      return connector;
      }
  
      private Connector ajpConnector() {
          Connector connector = new Connector("AJP/1.3");
          connector.setScheme("http");
          connector.setPort(ajpPort);
          connector.setSecure(false);
  
          return connector;
      }
  
  }
  </code></pre>
  
  <p>So based on the above, I have the following question:</p>
  
  <p>Is there any way to have Spring omit the creation of it's default connector? I have tried looking around and on their documentation but I have not found anything helpful. Since I'm launching an AJP connector (added as an additional one) I do not want the default HTTP one to be in place.</p>
  
  <p>Furthermore, when I attempt to create the redirect connector to handle HTTPS requests, my application won't start due to same port binding (the default connector listens on the same port as the redirect one).</p>
  
  <p>This setup was easy enough to do on pre-2.0 Spring boot versions but for some reason it's not that easy with the new changes.</p>
  
  <p>Does anyone have any solutions on how to do this? I would like to have overall control and programmatically configure anything.</p>