Skip to content

Commit b770ffc

Browse files
committed
Fully prepare connection when using SSL bundle
Fixes gh-36007
1 parent 7266d48 commit b770ffc

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ private static class SimpleClientHttpsRequestFactory extends SimpleClientHttpReq
245245

246246
@Override
247247
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
248+
super.prepareConnection(connection, httpMethod);
248249
if (this.sslBundle != null && connection instanceof HttpsURLConnection secureConnection) {
249250
SSLSocketFactory socketFactory = this.sslBundle.createSslContext().getSocketFactory();
250251
secureConnection.setSSLSocketFactory(socketFactory);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/AbstractClientHttpRequestFactoriesTests.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616

1717
package org.springframework.boot.web.client;
1818

19+
import java.io.IOException;
1920
import java.net.URI;
2021
import java.nio.charset.StandardCharsets;
2122
import java.time.Duration;
2223

2324
import javax.net.ssl.SSLHandshakeException;
2425

26+
import jakarta.servlet.ServletException;
27+
import jakarta.servlet.http.HttpServlet;
28+
import jakarta.servlet.http.HttpServletRequest;
29+
import jakarta.servlet.http.HttpServletResponse;
2530
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.CsvSource;
2633

2734
import org.springframework.boot.ssl.SslBundle;
2835
import org.springframework.boot.ssl.SslBundleKey;
@@ -94,16 +101,18 @@ void getReturnsRequestFactoryWithConfiguredReadTimeout() {
94101
assertThat(readTimeout((T) requestFactory)).isEqualTo(Duration.ofSeconds(120).toMillis());
95102
}
96103

97-
@Test
98-
void connectWithSslBundle() throws Exception {
104+
@ParameterizedTest
105+
@CsvSource({ "GET", "POST" })
106+
void connectWithSslBundle(String httpMethod) throws Exception {
99107
TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0);
100108
Ssl ssl = new Ssl();
101109
ssl.setClientAuth(ClientAuth.NEED);
102110
ssl.setKeyPassword("password");
103111
ssl.setKeyStore("classpath:test.jks");
104112
ssl.setTrustStore("classpath:test.jks");
105113
webServerFactory.setSsl(ssl);
106-
WebServer webServer = webServerFactory.getWebServer();
114+
WebServer webServer = webServerFactory
115+
.getWebServer((context) -> context.addServlet("test", TestServlet.class).addMapping("/"));
107116
try {
108117
webServer.start();
109118
int port = webServer.getPort();
@@ -118,9 +127,9 @@ void connectWithSslBundle() throws Exception {
118127
SslBundle sslBundle = SslBundle.of(stores, SslBundleKey.of("password"));
119128
ClientHttpRequestFactory secureRequestFactory = ClientHttpRequestFactories
120129
.get(ClientHttpRequestFactorySettings.DEFAULTS.withSslBundle(sslBundle));
121-
ClientHttpRequest secureRequest = secureRequestFactory.createRequest(uri, HttpMethod.GET);
130+
ClientHttpRequest secureRequest = secureRequestFactory.createRequest(uri, HttpMethod.valueOf(httpMethod));
122131
String secureResponse = StreamUtils.copyToString(secureRequest.execute().getBody(), StandardCharsets.UTF_8);
123-
assertThat(secureResponse).contains("HTTP Status 404 – Not Found");
132+
assertThat(secureResponse).contains("Received " + httpMethod + " request to /");
124133
}
125134
finally {
126135
webServer.stop();
@@ -131,4 +140,13 @@ void connectWithSslBundle() throws Exception {
131140

132141
protected abstract long readTimeout(T requestFactory);
133142

143+
public static class TestServlet extends HttpServlet {
144+
145+
@Override
146+
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
147+
res.getWriter().println("Received " + req.getMethod() + " request to " + req.getRequestURI());
148+
}
149+
150+
}
151+
134152
}

0 commit comments

Comments
 (0)