Skip to content

Commit fe536bf

Browse files
committed
Allow SSL to be used with Tomcat's Http11Nio2Protocol
Fixes gh-41007
1 parent 217c2c8 commit fe536bf

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizer.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import org.apache.commons.logging.Log;
2121
import org.apache.coyote.ProtocolHandler;
2222
import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
23-
import org.apache.coyote.http11.Http11NioProtocol;
2423
import org.apache.tomcat.util.net.SSLHostConfig;
2524
import org.apache.tomcat.util.net.SSLHostConfigCertificate;
2625
import org.apache.tomcat.util.net.SSLHostConfigCertificate.Type;
@@ -104,7 +103,7 @@ private void applySslBundle(SslBundle sslBundle, AbstractHttp11JsseProtocol<?> p
104103
String ciphers = StringUtils.arrayToCommaDelimitedString(options.getCiphers());
105104
sslHostConfig.setCiphers(ciphers);
106105
}
107-
configureSslStoreProvider(protocol, sslHostConfig, certificate, stores);
106+
configureSslStores(sslHostConfig, certificate, stores);
108107
configureEnabledProtocols(sslHostConfig, options);
109108
}
110109

@@ -119,10 +118,8 @@ private void configureSslClientAuth(SSLHostConfig config) {
119118
config.setCertificateVerification(ClientAuth.map(this.clientAuth, "none", "optional", "required"));
120119
}
121120

122-
private void configureSslStoreProvider(AbstractHttp11JsseProtocol<?> protocol, SSLHostConfig sslHostConfig,
123-
SSLHostConfigCertificate certificate, SslStoreBundle stores) {
124-
Assert.isInstanceOf(Http11NioProtocol.class, protocol,
125-
"SslStoreProvider can only be used with Http11NioProtocol");
121+
private void configureSslStores(SSLHostConfig sslHostConfig, SSLHostConfigCertificate certificate,
122+
SslStoreBundle stores) {
126123
try {
127124
if (stores.getKeyStore() != null) {
128125
certificate.setCertificateKeystore(stores.getKeyStore());

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,7 @@
6060
import org.apache.catalina.valves.RemoteIpValve;
6161
import org.apache.coyote.ProtocolHandler;
6262
import org.apache.coyote.http11.AbstractHttp11Protocol;
63+
import org.apache.coyote.http11.Http11Nio2Protocol;
6364
import org.apache.hc.client5.http.HttpHostConnectException;
6465
import org.apache.hc.client5.http.classic.HttpClient;
6566
import org.apache.hc.client5.http.impl.classic.HttpClients;
@@ -682,6 +683,20 @@ void shouldUpdateSslWhenReloadingSslBundles() throws Exception {
682683
assertThat(verifier.getLastPrincipal()).isEqualTo("CN=2");
683684
}
684685

686+
@Test
687+
void sslWithHttp11Nio2Protocol() throws Exception {
688+
TomcatServletWebServerFactory factory = getFactory();
689+
addTestTxtFile(factory);
690+
factory.setProtocol(Http11Nio2Protocol.class.getName());
691+
factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
692+
this.webServer = factory.getWebServer();
693+
this.webServer.start();
694+
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
695+
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
696+
HttpComponentsClientHttpRequestFactory requestFactory = createHttpComponentsRequestFactory(socketFactory);
697+
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
698+
}
699+
685700
@Override
686701
protected JspServlet getJspServlet() throws ServletException {
687702
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();

0 commit comments

Comments
 (0)