2012-08-06 43 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);