嵌入Tomcat-7仅在https中运行

时间:2012-08-06 07:31:11

标签: java tomcat7

我想运行仅使用HTTPS(8443)的嵌入式tomcat。我根本不想使用8080端口。 有什么想法吗?


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

由于

3 个答案:

答案 0 :(得分:2)

您必须删除[tomcat-dir] /conf/server.xml中定义的连接器,该连接器将其绑定到8080并具有单独的HTTPS连接器。

答案 1 :(得分:0)

我只是尝试使用问题中的代码段来创建httpsConnector并且效果很好!除了我必须添加一个缺失的行:

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

设置creating the keystorekeytool时设置的密码。

谢谢!

答案 2 :(得分:0)

从Tomcat实例获取defaultConnector并将其设置为https。这样就没有其他连接器了:

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