Skip to content

Commit

Permalink
Provide RestClientSsl as a bean
Browse files Browse the repository at this point in the history
Closes gh-37400
  • Loading branch information
mhalbritter committed Sep 15, 2023
1 parent 827471b commit 73c25d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
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;
Expand All @@ -44,7 +47,7 @@
* @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 {
Expand All @@ -57,6 +60,13 @@ HttpMessageConvertersRestClientCustomizer httpMessageConvertersRestClientCustomi
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -59,6 +60,12 @@ void shouldSupplyBeans() {
});
}

@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) -> {
Expand Down

0 comments on commit 73c25d7

Please sign in to comment.