之前的版本是:
protocol.setKeystoreFile(keystore.getAbsolutePath());
protocol.setKeystorePass("123qwe");
protocol.setKeyPass("123qwe");
新版本废弃了,HttpServletRequest 在 jakarta.servlet.http.HttpServletRequest 了。
不多说了
上代码:
private static Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
//InputStream stream = Main.class.getClassLoader().getResourceAsStream("tomcat1.keystore");
File keystore = new File("./tomcat1.keystore");
System.out.println("key path=" + keystore.getAbsolutePath()+", size="+keystore.length());
//inputstreamtofile(stream, keystore);
SSLHostConfig cfg = new SSLHostConfig();
cfg.setHostName("_default_");
SSLHostConfigCertificate certificate = new SSLHostConfigCertificate(cfg, Type.RSA);//new SSLHostConfigCertificate();
certificate.setCertificateKeyAlias("tomcat");
certificate.setCertificateKeyPassword("123qwe");
certificate.setCertificateKeystorePassword("123qwe");
certificate.setCertificateKeystoreFile(keystore.getAbsolutePath());
cfg.addCertificate(certificate);
connector.setScheme("https");
connector.setPort(AppCoreInitService.https_port);
protocol.setSSLEnabled(true);
protocol.setSecure(true);
protocol.addSslHostConfig(cfg);
// SSLHostConfig[] s_cfgs = protocol.findSslHostConfigs();
// int len = s_cfgs.length;
return connector;
} catch (Exception ex) {
throw new IllegalStateException("can't access keystore", ex);
}
}
用法:tomcat.setConnector(createSslConnector());
以下是我折腾过程中遇到的一些错:
Caused by: java.lang.IllegalArgumentException: No SSLHostConfig element was found with the hostName [_default_] to match the defaultSSLHostConfigName for the connector [https-jsse-nio-183
key path=D:\wd_git_20200703\test_maven_project\.\tomcat1.keystore, size=2257
十二月 17, 2020 12:35:01 下午 org.apache.tomcat.util.net.SSLHostConfig setProperty
警告: The property [truststorePassword] was set on the SSLHostConfig named [_default_] and is for the [JSSE] configuration syntax but the SSLHostConfig is being used with the [OPENSSL] configuration syntax