嵌入式Tomcat启用SSL [英] Embedded Tomcat enable SSL
问题描述
我正在尝试为嵌入式Tomcat设置SSL。两个连接器都启动但我只得到http的响应。在https上,当我尝试 http:// localhost:9000 /
端口打开时,我在Chrome中收到无数据收到消息:
我试过telnet
telnet localhost 9000
我有一个连接。
我也试过
openssl s_client -connect localhost:9000
和GET / method
我的servlet打印我控制台中的预期结果。我不明白为什么我在浏览器中遇到这个错误(chrome和Firefox)
我的操作系统是Ubuntu 14.04,我试过Java 7和Java 8都有相同的结果。 Tomcat版本是8.0.23来自Maven repo
代码是:
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;
}
}
您可以在 github
我是已经尝试过不同的密钥库,但结果相同。此外,密钥库看起来还不错: keytool -list -keystore keystore.jks
似乎符合预期。
提前致谢
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
推荐答案
原来是我的错。该服务已启动并运行但我继续尝试 http:// localhost:9000 而不是 https:// locahost:9000
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
这篇关于嵌入式Tomcat启用SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!