fix TomcatEngine to support https

This commit is contained in:
Looly
2024-12-24 23:25:57 +08:00
parent b4eb357775
commit cacfbd9fc8
13 changed files with 252 additions and 32 deletions

View File

@@ -56,6 +56,12 @@
<artifactId>hutool-log</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.dromara.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<!-- webservice SOAP 从javaEE变成jakartaEEjavax.xml.soap Jakarta XML SOAP(jakarta.xml.soap) -->
<dependency>
<groupId>jakarta.xml.soap</groupId>

View File

@@ -16,7 +16,10 @@
package org.dromara.hutool.http.server;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
/**
* 服务器配置
@@ -115,6 +118,18 @@ public class ServerConfig {
return sslContext;
}
/**
* 设置证书库<br>
* 此方法和{@link #setSslContext(SSLContext)}互斥
*
* @param keyStore 证书库
* @param passwd 密码
* @return this
*/
public ServerConfig setKeystore(final KeyStore keyStore, final char[] passwd) {
return setSslContext(SSLContextUtil.createSSLContext(keyStore, passwd));
}
/**
* 设置SSL上下文
*

View File

@@ -125,20 +125,34 @@ public class TomcatEngine extends AbstractServerEngine {
// SSL配置
final SSLContext sslContext = config.getSslContext();
if(null != sslContext){
final SSLHostConfig sslHostConfig = new SSLHostConfig();
final SSLHostConfigCertificate sslHostConfigCertificate =
new SSLHostConfigCertificate(sslHostConfig, SSLHostConfigCertificate.Type.RSA);
sslHostConfigCertificate.setSslContext(new JSSESSLContext(sslContext));
sslHostConfig.addCertificate(sslHostConfigCertificate);
connector.addSslHostConfig(sslHostConfig);
protocol.setSSLEnabled(true);
protocol.setSecure(true);
protocol.addSslHostConfig(createSSLHostConfig(sslContext));
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(config.getPort());
}
return connector;
}
/**
* 创建SSL HostConfig
*
* @param sslContext SSLContext
* @return SSL HostConfig
*/
private static SSLHostConfig createSSLHostConfig(final SSLContext sslContext) {
final SSLHostConfig sslHostConfig = new SSLHostConfig();
final SSLHostConfigCertificate sslHostConfigCertificate =
new SSLHostConfigCertificate(sslHostConfig, SSLHostConfigCertificate.Type.RSA);
sslHostConfigCertificate.setSslContext(new JSSESSLContext(sslContext));
sslHostConfig.addCertificate(sslHostConfigCertificate);
return sslHostConfig;
}
/**
* 初始化Context
*

View File

@@ -1,12 +1,23 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import org.dromara.hutool.crypto.KeyStoreUtil;
import org.dromara.hutool.http.server.ServerConfig;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
public class JettyTest {
public static void main(final String[] args) {
final char[] pwd = "123456".toCharArray();
final KeyStore keyStore = KeyStoreUtil.readJKSKeyStore(FileUtil.file("d:/test/keystore.jks"), pwd);
// 初始化SSLContext
final SSLContext sslContext = SSLContextUtil.createSSLContext(keyStore, pwd);
final ServerEngine engine = ServerEngineFactory.createEngine("jetty");
engine.init(ServerConfig.of());
engine.init(ServerConfig.of().setSslContext(sslContext));
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Jetty response test");

View File

@@ -1,12 +1,23 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import org.dromara.hutool.crypto.KeyStoreUtil;
import org.dromara.hutool.http.server.ServerConfig;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
public class SunServerTest {
public static void main(String[] args) {
final char[] pwd = "123456".toCharArray();
final KeyStore keyStore = KeyStoreUtil.readJKSKeyStore(FileUtil.file("d:/test/keystore.jks"), pwd);
// 初始化SSLContext
final SSLContext sslContext = SSLContextUtil.createSSLContext(keyStore, pwd);
final ServerEngine engine = ServerEngineFactory.createEngine("SunHttpServer");
engine.init(ServerConfig.of());
engine.init(ServerConfig.of().setSslContext(sslContext));
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Sun Server response test");

View File

@@ -1,12 +1,23 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import org.dromara.hutool.crypto.KeyStoreUtil;
import org.dromara.hutool.http.server.ServerConfig;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
public class TomcatTest {
public static void main(String[] args) {
public static void main(final String[] args) throws Exception {
final char[] pwd = "123456".toCharArray();
final KeyStore keyStore = KeyStoreUtil.readJKSKeyStore(FileUtil.file("d:/test/keystore.jks"), pwd);
// 初始化SSLContext
final SSLContext sslContext = SSLContextUtil.createSSLContext(keyStore, pwd);
final ServerEngine engine = ServerEngineFactory.createEngine("tomcat");
engine.init(ServerConfig.of());
engine.init(ServerConfig.of().setSslContext(sslContext));
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Tomcat response test");

View File

@@ -16,13 +16,24 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import org.dromara.hutool.crypto.KeyStoreUtil;
import org.dromara.hutool.http.server.ServerConfig;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
public class UndertowTest {
public static void main(String[] args) {
final char[] pwd = "123456".toCharArray();
final KeyStore keyStore = KeyStoreUtil.readJKSKeyStore(FileUtil.file("d:/test/keystore.jks"), pwd);
// 初始化SSLContext
final SSLContext sslContext = SSLContextUtil.createSSLContext(keyStore, pwd);
final ServerEngine engine = ServerEngineFactory.createEngine("undertow");
engine.init(ServerConfig.of());
engine.init(ServerConfig.of().setSslContext(sslContext));
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Undertow response test");