Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Feign client to take truststore from custom property

Feign client in our app is communicating with a self-signed server. We are able to make Feign client use the custom truststore using the property javax.net.ssl.trustStore system property. But because my app also communicates with standard CA certified sites, the default truststore shouldn't be overridden.

How can I use the custom truststore without using javax.net.ssl.trustStore system property? Or else how can I have my Feign client use the truststore from a property other than standard javax.net.ssl.trustStore system property?

like image 868
Kannan Ramamoorthy Avatar asked Oct 15 '25 20:10

Kannan Ramamoorthy


1 Answers

This is how i used FeignClient with keystore and truststore

FeignClient Configuration

@Configuration
public class TestClientConfig {

    @Bean
    public Client feignClient() {
        Client trustSSLSockets = new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());
        return trustSSLSockets;
    }

    private SSLSocketFactory getSSLSocketFactory() {
        try {
            TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    //Do your validations
                    return true;
                }
            };
            String allPassword = "123456";
            SSLContext sslContext = SSLContextBuilder
                    .create()
                    // .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.p12"), allPassword.toCharArray(), allPassword.toCharArray())
                    .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"), allPassword.toCharArray(), allPassword.toCharArray())
                    .loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword.toCharArray())
                    .build();
            return sslContext.getSocketFactory();
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }
}

Interface

@FeignClient(name = "Test", url = "https://localhost:8443",configuration=TestClientConfig.class)
public interface TestClient {

    @RequestMapping(method = RequestMethod.GET,value = "/hello")
    String getHello();
}
like image 50
Niraj Sonawane Avatar answered Oct 18 '25 09:10

Niraj Sonawane