tomcat-embed-core 10.0.0 https 监听

之前的版本是:

            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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值