eclipse / microprofile-rest-client

MicroProfile Rest Client
Apache License 2.0
142 stars 72 forks source link

Setting SSLContext for CDI injected clients #344

Closed Baddy247 closed 9 months ago

Baddy247 commented 2 years ago

Hi,

I have a CDI injected rest client using @RegisterRestClient. I am planning to implement mutual auth/2-way auth for this client and I was wondering how to initialize the SSLContext object. From the documentation I only see it is possible for RestClientBuilder. I'd appreciate any help.

andymc12 commented 2 years ago

Hi - and sorry for the super-slow response - after switching jobs, I haven't had a lot of spare time for MP issues.

The short answer is that for now, you're stuck with either using the RestClientBuilder or using the MP Config properties - which only allow you to specify the keystore, truststore and hostname verifier (https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#ssl).

That said, it may be possible to use CDI semantics to produce the client instance specifying the SSLContext that you want - something like:

public interface MyClient {
    @GET
    Widget getWidget(...);
}

@ApplicationScoped
public class MyBeanThatUsesARestClient {

    @Inject
    MyClient client;
}

@ApplicationScoped
public class SomeOtherBean {

    @Produces
    public MyClient createNewRestClientWithSSLSettings() {
        return RestClientBuilder.newBuilder().baseUri(SOME_URI).sslContext(SOME_SSL_CONTEXT).build();
    }
}

HTH

AhmedEzzat12 commented 1 year ago

Thanks @andymc12 that did work!

Emily-Jiang commented 9 months ago

Based on the above comments, we are going to close this as there are no better solution than what @andymc12 recommended.