Browse Source

Provide RestClientSsl as a bean

Closes gh-37400
pull/37434/head
Moritz Halbritter 2 years ago
parent
commit
73c25d7156
  1. 12
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java
  2. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientSsl.java
  3. 7
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java

12
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java

@ -19,10 +19,13 @@ package org.springframework.boot.autoconfigure.web.client; @@ -19,10 +19,13 @@ package org.springframework.boot.autoconfigure.web.client;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.web.client.RestClientCustomizer;
@ -44,7 +47,7 @@ import org.springframework.web.client.RestClient; @@ -44,7 +47,7 @@ import org.springframework.web.client.RestClient;
* @author Moritz Halbritter
* @since 3.2.0
*/
@AutoConfiguration(after = HttpMessageConvertersAutoConfiguration.class)
@AutoConfiguration(after = { HttpMessageConvertersAutoConfiguration.class, SslAutoConfiguration.class })
@ConditionalOnClass(RestClient.class)
@Conditional(NotReactiveWebApplicationCondition.class)
public class RestClientAutoConfiguration {
@ -57,6 +60,13 @@ public class RestClientAutoConfiguration { @@ -57,6 +60,13 @@ public class RestClientAutoConfiguration {
return new HttpMessageConvertersRestClientCustomizer(messageConverters.getIfUnique());
}
@Bean
@ConditionalOnMissingBean(RestClientSsl.class)
@ConditionalOnBean(SslBundles.class)
AutoConfiguredRestClientSsl restClientSsl(SslBundles sslBundles) {
return new AutoConfiguredRestClientSsl(sslBundles);
}
@Bean
@ConditionalOnMissingBean
RestClientBuilderConfigurer restClientBuilderConfigurer(ObjectProvider<RestClientCustomizer> customizerProvider) {

2
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientSsl.java

@ -32,7 +32,7 @@ import org.springframework.web.client.RestClient; @@ -32,7 +32,7 @@ import org.springframework.web.client.RestClient;
* Typically used as follows: <pre class="code">
* &#064;Bean
* public MyBean myBean(RestClient.Builder restClientBuilder, RestClientSsl ssl) {
* RestClient restClientrestClient= restClientBuilder.apply(ssl.forBundle("mybundle")).build();
* RestClient restClientrestClient= restClientBuilder.apply(ssl.fromBundle("mybundle")).build();
* return new MyBean(webClient);
* }
* </pre> NOTE: Apply SSL configuration will replace any previously

7
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java

@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.boot.web.codec.CodecCustomizer;
@ -59,6 +60,12 @@ class RestClientAutoConfigurationTests { @@ -59,6 +60,12 @@ class RestClientAutoConfigurationTests {
});
}
@Test
void shouldSupplyRestClientSslIfSslBundlesIsThere() {
this.contextRunner.withBean(SslBundles.class, () -> mock(SslBundles.class))
.run((context) -> assertThat(context).hasSingleBean(RestClientSsl.class));
}
@Test
void shouldCreateBuilder() {
this.contextRunner.run((context) -> {

Loading…
Cancel
Save