Self Signed Certificate in Spring 5 WebClient

Carvia Tech | May 05, 2019 | 1 min read | 407 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. Custom banner in spring boot application
  2. What is new in Spring Boot 2
  3. Setting a Random Port in Spring Boot Application at startup
  4. How to prevent duplicate form submission in Spring MVC
  5. Spring Boot 2.0 Reactive Web Performance Metrics
  6. Prevent Lost Updates in Database Transaction using Spring Hibernate
  7. What are different Bean Scopes in Spring?

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