Self Signed Certificate in Spring 5 WebClient

Carvia Tech | May 05, 2019 | 1 min read | 376 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. What is new in Spring Boot 2
  2. Setting a Random Port in Spring Boot Application at startup
  3. How to prevent duplicate form submission in Spring MVC
  4. Spring Boot 2.0 Reactive Web Performance Metrics
  5. What are different Bean Scopes in Spring?
  6. Running Spring Boot app as a service in unix
  7. Run method on application startup in Spring Boot

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 month ago

Recommended books for interview preparation:

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