Self Signed Certificate in Spring 5 WebClient

Carvia Tech | November 23, 2019 | 1 min read | 2,856 views | Spring Boot 2

We can use an insecure TrustManagerFactory that trusts all X.509 certificates without any verification. This will allow any https certificate (self-signed, expired, etc) with WebClient communication.

A WebClient that uses this insecure TrustManagerFactory can be created like shown in below code:

Creating WebClient Bean
public WebClient createWebClient() throws SSLException {
    SslContext sslContext = SslContextBuilder
    ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt -> opt.sslContext(sslContext));
    return WebClient.builder(). clientConnector(httpConnector).build();

Now you can use this WebClient instance to make calls to a server that has self-signed certificate:

Making Call
public void getUrl(String baseUrl) {
        .uri(b -> b.path("/").queryParam("name", "foo").build())
        .subscribe(success -> processSuccess(success, baseUrl), throwable -> processError(throwable, baseUrl));

Never use this TrustManagerFactory in production. It is purely for testing purposes, and thus it is very insecure.

Top articles in this category:
  1. Disable SSL certificate validation in RestTemplate
  2. Spring Boot file upload with Kotlin and Junit 5
  3. Testing web layer in Spring Boot using WebMvcTest
  4. Send Gupshup SMS using Java API
  5. Basic Auth Security in Spring Boot 2
  6. What is new in Spring Boot 2
  7. SendGrid emails with Spring Boot and Java

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info