• Home
  • Using ssl certificate with feign – Java

Using ssl certificate with feign – Java

How to use ssl certificate with feign? Know the answer here – To use an SSL certificate with Feign, you can configure your Feign client with an SSLSocketFactory that trusts the SSL certificate. You can do this by creating an OkHttpClient with an X509TrustManager that trusts the SSL certificate and then passing this client to the Feign builder:

X509TrustManager trustManager = ...; // Create trust manager that trusts the SSL certificate
SSLSocketFactory sslSocketFactory = new SSLSocketFactoryCompat(trustManager);

OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)

GitHub github = Feign.builder()
.client(new OkHttpClient(client))
.target(GitHub.class, "https://api.github.com");

Alternatively, you can also use a Client.Default instance with the OkHttpClient as the delegate:

GitHub github = Feign.builder()
.client(new Client.Default(client, null))
.target(GitHub.class, "https://api.github.com");

Here, GitHub is the interface defining the Feign client and https://api.github.com is the base URL of the API being called. With these configurations in place, the Feign client will use the provided SSLSocketFactory and trust the SSL certificate when making requests to the API.