The api to add https support to an embedded tomcat server follows the corresponding server.xml elements pretty closely. For example:
Connector httpsConnector = new Connector();
httpsConnector.setPort(443);
httpsConnector.setSecure(true);
httpsConnector.setScheme("https");
httpsConnector.setAttribute("keyAlias", keyAlias);
httpsConnector.setAttribute("keystorePass", password);
httpsConnector.setAttribute("keystoreFile", keystorePath);
httpsConnector.setAttribute("clientAuth", "false");
httpsConnector.setAttribute("sslProtocol", "TLS");
httpsConnector.setAttribute("SSLEnabled", true);
Tomcat tomcat = new Tomcat();
//...
Service service = tomcat.getService();
service.addConnector(httpsConnector);
To add a redirect to your http port:
Connector defaultConnector = tomcat.getConnector();
defaultConnector.setRedirectPort(443);
If you only want an https port with no other port open, you can call setConnnector() to make your connector the default on the tomcat object instead of adding new ones to the service.
In the example above “clientAuth” is set to “false” (a string) whereas “SSLEnabled” is set to true (boolean). Is this correct?
I believe either string or boolean is fine. The ‘clientAuth’ attribute itself refers to whether to require a client certificate or not.