Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore SSL cert trust errors in Feign?

How can I achieve curl -k in feign client?

I know I can do this. Just want to know if there's a way to ignore or disable.

new Client.Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier)
like image 719
Bee Avatar asked Apr 17 '17 07:04

Bee


People also ask

How do I ignore SSL error?

To bypass SSL certificate validation for local and test servers, you can pass the -k or --insecure option to the Curl command. This option explicitly tells Curl to perform "insecure" SSL connections and file transfers. Curl will ignore any security warnings about an invalid SSL certificate and accept it as valid.


2 Answers

With current versions of spring-cloud-starter-openfeign suppressing hostname verification works as follows.

When using apache httpclient:

In application.yml set disable-ssl-validation property

feign.httpclient.disable-ssl-validation: true

In pom.xml add feign-httpclient dependency.

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>

If you prefer okhttp you must enable okhttp with another application property and add feign-okhttp dependency:

feign.httpclient.disableSslValidation=true
feign.httpclient.enabled=false
feign.okhttp.enabled=true

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

For httpclient5 (hc5), property disable-ssl-validation sadly does not turn off hostname verification (yet?), here's the ticket: https://github.com/spring-cloud/spring-cloud-openfeign/issues/625

Application properties for enabling hc5.

feign.httpclient.disableSslValidation=true
feign.httpclient.hc5.enabled=true

Maven dependency to add

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-hc5</artifactId>
</dependency>

NOTE: The tricky part for me was that I missed to add feign-httpclient as a dependency. In this case, a default feign client with enabled hostname verification is used.

like image 129
Bertolt Avatar answered Nov 16 '22 02:11

Bertolt


Override via feign configuration

@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 {
                return true;
            }
        };

        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        return sslContext.getSocketFactory();
    } catch (Exception exception) {
    }
    return null;
}
like image 43
eranda.del Avatar answered Nov 16 '22 01:11

eranda.del