Embed Tomcat-7 to run only in https

I want to run a built-in tomcat that uses only HTTPS (8443). I do not want to use port 8080. Any ideas on?

     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

+6
source share
3 answers

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

+2
source

I just tried using the snippet in the issue of creating httpsConnector and it works great! In addition, I had to add one missing line:

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

Setting up this password that I set when creating a keystore using keytool did the trick.

Thanks!

0
source

Get the default constructor from the Tomcat instance and configure it for https. So 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); 
0
source

Source: https://habr.com/ru/post/922194/


All Articles