6
votes

I want to run embedded tomcat that uses only HTTPS (8443). I do not want 8080 port to be used at all. Any idea about ?


    Connector httpsConnector = new Connector();
    httpsConnector.setPort(httpsPort);
    httpsConnector.setSecure(true);
    httpsConnector.setScheme("https");
    httpsConnector.setAttribute("keystoreFile", appBase + "/.keystore");
    httpsConnector.setAttribute("clientAuth", "false");
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("SSLEnabled", true);

    Tomcat tomcat = new Tomcat();
    tomcat.getService().addConnector(httpsConnector);
    tomcat.setPort(8080);
    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setRedirectPort(8443);

    tomcat.setBaseDir(".");
    tomcat.getHost().setAppBase(appBase);

    StandardServer server = (StandardServer) tomcat.getServer();
    AprLifecycleListener listener = new AprLifecycleListener();
    server.addLifecycleListener(listener);

Thanks

3
Did you manage to forbid the 8080 port?Vojtěch

3 Answers

2
votes

You would have to remove the connector defined in [tomcat-dir]/conf/server.xml which binds it to 8080 and have a separate connector for HTTPS.

0
votes

I just tried using the snippet in the question for creating the httpsConnector and it worked great! Except I had to add one missing line:

httpsConnector.setAttribute("keystorePass", "YOUR-PASSWORD-HERE");

Setting that to the password I setup when creating the keystore with keytool did the trick.

Thanks!

0
votes

Get the defaultConnector from the Tomcat instance and set it up for https. In this way there is no other connector:

    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setPort(8443);
    defaultConnector.setSecure(true);
    defaultConnector.setScheme("https");
    defaultConnector.setAttribute("keystorePass", "password");
    defaultConnector.setAttribute("keystoreFile", absolutePath + "/keystore.jks");
    defaultConnector.setAttribute("clientAuth",  "false");
    defaultConnector.setAttribute("sslProtocol",  "TLS");
    defaultConnector.setAttribute("SSLEnabled",  true);