Description
Spring Boot Version: 3.1.0
Java version: 17
When using newly (in 3.1.0) added support for SSLBundles, all HTTP requests made via RestTemplate (org.springframework.web.client.RestTemplate) are sent as GET requests, even if the request was made as other Http method.
For example, RestTemplate call like this (POST)
restTemplate.postForEntity(httpRequest.getUrl(), httpPayload, httpRequest.getReturnClass());
is logged as GET request on remote NGINX web server.
RestTemplate is registered as Bean with following:
@Bean
public RestTemplate configureRestTemplate(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles)
{
return restTemplateBuilder
.setSslBundle(sslBundles.getBundle("server"))
.build();
}
From what i could see from debugging, this method call checks for ssl bundle, but does not call setRequestMethod()
on the connection object:
ClientHttpRequestFactories.prepareConnection()
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
if (this.sslBundle != null && connection instanceof HttpsURLConnection secureConnection) {
SSLSocketFactory socketFactory = this.sslBundle.createSslContext().getSocketFactory();
secureConnection.setSSLSocketFactory(socketFactory);
}
}
For reference, default prepareConnection()
implementation that is invoked when SslBundles are not used (different RequestFactory implementation):
SimpleClientHttpRequestFactory.prepareConnection()
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
if (this.connectTimeout >= 0) {
connection.setConnectTimeout(this.connectTimeout);
}
if (this.readTimeout >= 0) {
connection.setReadTimeout(this.readTimeout);
}
connection.setDoInput(true);
if ("GET".equals(httpMethod)) {
connection.setInstanceFollowRedirects(true);
}
else {
connection.setInstanceFollowRedirects(false);
}
if ("PUT".equals(httpMethod) || "POST".equals(httpMethod)) {
connection.setDoOutput(true);
}
else {
connection.setDoOutput(false);
}
connection.setRequestMethod(httpMethod); // <- set request method
}