6

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

Srinivas
  • 390
  • 2
  • 11

3 Answers3

2

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.

Chris
  • 5,584
  • 9
  • 40
  • 58
  • Actually i am embedding tomcat, i don't use server.xml there. Programmatically connector will be added – Srinivas Aug 06 '12 at 07:36
  • How is your tomcat starting up ? Can you provide more details in your question ? – Chris Aug 06 '12 at 07:40
  • @Srinivas Not sure I understand completely. what do you clearly mean by **embedding** ? Are you creating a customized server of your own ? Even if you manage to start this **embedded** server, how are deployments/classloading being handled ? Inshort, What is your final goal ? – Chris Aug 06 '12 at 08:13
  • I will start/stop tomcat server from a Custom Java Class, where i will mention which web applications need to be deployed. As you can see from the above code Tomcat's DefaultConnector will be http, but i don't want that connector. I will create new HTTPS Connector. So finally i want to run Tomcat in only 8443 port (https) – Srinivas Aug 06 '12 at 12:13
0

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!

mckamey
  • 17,359
  • 16
  • 83
  • 116
0

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);