2012-08-06 89 views
6

我想运行仅使用HTTPS(8443)的嵌入式tomcat。我根本不需要8080端口。 有什么想法?将Tomcat-7嵌入到仅在https中运行

 

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

由于

+0

你设法禁止8080端口吗? – 2013-07-24 22:37:56

回答

2

你将不得不除去其结合到在8080 [Tomcat的DIR] /conf/server.xml限定的连接器和具有用于HTTPS单独的连接器。

+0

其实我嵌入tomcat,我不使用server.xml那里。将以编程方式连接器将被添加 – Srinivas 2012-08-06 07:36:19

+0

你的tomcat是如何启动的?你能否在你的问题中提供更多细节? – Chris 2012-08-06 07:40:44

+0

已编辑的帖子包含嵌入代码 – Srinivas 2012-08-06 07:41:39

0

我刚刚尝试在创建httpsConnector的问题中使用该代码段,它效果很好!除非我不得不添加一个缺失行:

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

是设置为密码我安装时creating the keystorekeytool的伎俩。

谢谢!

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