Search code examples
javaspringspring-cloud-netflixspring-cloud-feign

How to set HostnameVerifier in Feign-Client from spring-cloud-netflix-feign


I'm trying to setup my Spring Cloud Feign Client to use custom HostnameVerifier. I need the custom HostnameVerifier to ignore certificate problems. How can I do that?

here is my current config:

@FeignClient(name = "AccountSettingsClient", url = "${account.settings.service.url}", decode404 = true,
        configuration = AccountSettingsClientConfig.class, fallbackFactory = AccountSettingsClientFallbackFactory.class)
public interface AccountSettingsClient {
    @RequestMapping(method = RequestMethod.GET, value = "/settings/{uuid}")
    AccountSettings accountSettings(@PathVariable("uuid") String uuid);
}

@Component
@Slf4j
class AccountSettingsClientFallbackFactory implements FallbackFactory<AccountSettingsClient> {
    @Override
    public AccountSettingsClient create(Throwable cause) {
        return uuid -> {
            log.warn("Falling back to null.", cause);
            return null;
        };
    }
}

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(SomeProperties.class)
@EnableFeignClients
public class AccountSettingsClientConfig {
     private final SomeProperties someProperties;

     @Bean
     RequestInterceptor oauth2FeignRequestInterceptor() {
         return new OAuth2FeignRequestInterceptor(new 
            DefaultOAuth2ClientContext(), resource());
     }
}

Solution

  • By default there gets created a LoadBalancerFeignClient with a HttpURLConnection on board and can't override settings of HostnameVerifier of it. In order to override it choose another client like OkHttp or Apache Http Client, add correspondent maven dependency and then you can override the client with all settings.

    I chose the OkHttpClient and added to my AccountSettingsConfig follow bean:

    @Bean
    public okhttp3.OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder().hostnameVerifier((s, sslSession) -> true)
                .build();
    }