Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Tomcat enable SSL

I'm trying to setup SSL for embedded Tomcat. Both connectors starts but I only get response on http. On https I get in chrome a "No data received message" when I try http://localhost:9000/ The port is open: I've tried telnet telnet localhost 9000 and I have a connection. I've also tried openssl s_client -connect localhost:9000 and GET / method and my servlet prints me the expected result in console. I do not understand why I get this error in browsers(chrome and Firefox) My OS is Ubuntu 14.04 and I've tried with both Java 7 and Java 8 having the same result. Tomcat version is 8.0.23 from Maven repo The code is:

public class Main {

public static void main(String[] args) throws Exception {
    Tomcat tomcat = new Tomcat();
    Service service = tomcat.getService();
    service.addConnector(getSslConnector());


    File base = new File(System.getProperty("java.io.tmpdir"));
    Context rootCtx = tomcat.addContext("/", base.getAbsolutePath());
    Tomcat.addServlet(rootCtx, "emptyServlet", new EmptyServlet());
    rootCtx.addServletMapping("/*", "emptyServlet");
    tomcat.start();
    tomcat.getServer().await();

}

private static Connector getSslConnector() {
    Connector connector = new Connector();
    connector.setPort(9000);
    connector.setSecure(true);
    connector.setScheme("https");
    connector.setAttribute("keyAlias", "tomcat");
    connector.setAttribute("keystorePass", "password");
    connector.setAttribute("keystoreType", "JKS");
    connector.setAttribute("keystoreFile",
            "keystore.jks");
    connector.setAttribute("clientAuth", "false");
    connector.setAttribute("protocol", "HTTP/1.1");
    connector.setAttribute("sslProtocol", "TLS");
    connector.setAttribute("maxThreads", "200");
    connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");
    connector.setAttribute("SSLEnabled", true);
    return connector;
 }
}

The keystore you can find it on github

I've already tried different keystores but with the same result. Also the keystore looks good: keytool -list -keystore keystore.jks seems to be as expected. Thanks in advance

like image 210
Daniel Jipa Avatar asked Jun 24 '15 18:06

Daniel Jipa


1 Answers

It turned out to be my fault. The service was up and running but I kept on trying on http://localhost:9000 not https://locahost:9000 in my browser

like image 162
Daniel Jipa Avatar answered Oct 22 '22 19:10

Daniel Jipa