Java 嵌入Tomcat-7以仅在https中运行

Java 嵌入Tomcat-7以仅在https中运行,java,tomcat7,Java,Tomcat7,我想运行只使用HTTPS(8443)的嵌入式tomcat。我根本不想使用8080端口。 你知道吗 Connector httpsConnector = new Connector(); httpsConnector.setPort(httpsPort); httpsConnector.setSecure(true); httpsConnector.setScheme("https"); httpsConnector.setAttribute("keystoreFi

我想运行只使用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); 连接器httpsConnector=新连接器(); httpsConnector.setPort(httpsPort); httpsConnector.setSecure(真); httpsConnector.setScheme(“https”); setAttribute(“keystrefile”,appBase+“/.keystore”); setAttribute(“clientAuth”、“false”); httpsConnector.setAttribute(“sslProtocol”、“TLS”); httpsConnector.setAttribute(“SSLEnabled”,true); Tomcat Tomcat=新的Tomcat(); tomcat.getService().addConnector(httpsConnector); tomcat.setPort(8080); 连接器defaultConnector=tomcat.getConnector(); defaultConnector.setRedirectPort(8443); tomcat.setBaseDir(“.”); tomcat.getHost().setAppBase(appBase); 标准服务器=(标准服务器)tomcat.getServer(); AprLifecycleListener侦听器=新的AprLifecycleListener(); addLifecycleListener(侦听器);
谢谢

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

我刚刚尝试使用问题中的代码片段创建
httpsConnector
,效果非常好!除了我必须加上一行:

httpsConnector.setAttribute("keystorePass", "YOUR-PASSWORD-HERE");
使用
keytool
时,将其设置为我设置的密码


谢谢

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

实际上,我是在嵌入tomcat,我没有在那里使用server.xml。将以编程方式添加连接器tomcat是如何启动的?你能在你的问题中提供更多细节吗?@Srinivas不确定我是否完全理解。你所说的嵌入是什么意思?您是否正在创建自己的自定义服务器?即使您设法启动了这个嵌入式服务器,部署/类加载是如何处理的?总之,您的最终目标是什么?我将从一个自定义Java类启动/停止tomcat服务器,在这里我将提到需要部署哪些web应用程序。从上面的代码中可以看到,Tomcat的DefaultConnector将是http,但我不想要那个连接器。我将创建新的HTTPS连接器。最后,我想只在8443端口(https)上运行Tomcat。您是否设法禁止8080端口?