0

How I can inject a custom sslContext by use the @RegisterRestClient inside a interface ?

stuckoverflow
  • 625
  • 2
  • 7
  • 23
ThomasL
  • 1
  • 1

1 Answers1

0

Nor Quarkus or Microprofile allow you to programmatically build the rest client (that's the purpose of providing you a interface auto-implemented afterall). You could nevertheless implement by yourself the rest-client interface and set your javax.net.ssl.SSLContext.

The example below is meant to provide a SSLContext trusting all hosts:

package org.me.rest;

import java.net.URI;
import java.security.KeyManagementException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.me.rest.MyRestClient;

@Priority(value = 1) //choose an appropriate value here
@RestClient
@ApplicationScoped
public class MyRestClientImpl implements MyRestClient {

    @Override
    public Response getTheResource() {

        SSLContext sslContext = null;

        try {

            sslContext = SSLContext.getInstance("TLS");
            TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            sslContext.init(null, new TrustManager[] {
                tm
            }, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        AuthRestClient client =
            RestClientBuilder.newBuilder().baseUri(URI.create("https://hostname:port/some/rest/resource/"))
            .hostnameVerifier(new NoopHostnameVerifier()).sslContext(sslContext).build(AuthRestClient.class);
        return client.getTheResource();
    }
}

giulianopz
  • 184
  • 1
  • 8