title: Spring Boot Jetty Auto Redirect HTTP (Port 80) requests to HTTPS (Port 8443)
body: <p>I have the following code to configure Jetty server:</p>
  
  <pre><code>@Configuration
  public class RedirectHttpToHttpsOnJetty2Config {
  
      @Bean
      public ConfigurableServletWebServerFactory webServerFactory() {
          JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
              factory.addServerCustomizers(new JettyServerCustomizer() {
  
                  @Override
                  public void customize(Server server) {
                      ServerConnector connector = new ServerConnector(server);
                      connector.setPort(80);
                      server.addConnector(connector);
                  }
              });
          return factory;
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <p>application.properties as</p>
  
  <pre><code>server.port=8443
  server.ssl.key-store=classpath:keystore
  server.ssl.key-store-password=xyzxyzxyz
  server.ssl.key-password=xyzxyzxyz
  </code></pre>
  
  <p>My applications works expectedly when I access localhost:8443 but localhost:80 is not reachable. gradlew bootRun mentions</p>
  
  <p>...
  <em>Jetty started on port(s) 8443 (ssl, http/1.1), 80 (http/1.1) with context path '/'</em>
  ...</p>
  
  <p>but upon visiting <a href="http://localhost:80" rel="nofollow noreferrer">http://localhost:80</a> I get the message</p>
  
  <p><em>This site can’t be reached... localhost refused to connect.</em></p>
  
  <p>I am looking for <a href="http://localhost:80" rel="nofollow noreferrer">http://localhost:80</a> getting redirected to <a href="https://localhost:8443" rel="nofollow noreferrer">https://localhost:8443</a>. </p>
  
  <p>I had it working in Tomcat:</p>
  
  <pre><code>    @Bean
      public ServletWebServerFactory servletContainer(){
          TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
              @Override
              protected void postProcessContext(Context context) {
                  SecurityConstraint securityConstraint = new SecurityConstraint();
                   securityConstraint.setUserConstraint("CONFIDENTIAL");
                   SecurityCollection collection = new SecurityCollection();
                   collection.addPattern("/*");
                   securityConstraint.addCollection(collection);
                   context.addConstraint(securityConstraint);
               }
           };
           tomcat.addAdditionalTomcatConnectors(redirectConnector());
           return tomcat;
       }
  
      private Connector redirectConnector(){
          Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
          connector.setScheme("http");
          connector.setPort(80);
          connector.setSecure(false);
          connector.setRedirectPort(8443);
          return connector;
      }
  </code></pre>
  
  <p>But unable to find an equivalent for Jetty. Any pointers much appreciated.</p>