2

我正在尝试将我的 Spring Cloud Feign Client 设置为使用自定义 HostnameVerifier。我需要自定义 HostnameVerifier 来忽略证书问题。我怎样才能做到这一点?

这是我当前的配置:

@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());
     }
}
4

1 回答 1

2

默认情况下,会创建一个带有 HttpURLConnection 的 LoadBalancerFeignClient,并且不能覆盖它的 HostnameVerifier 设置。为了覆盖它,请选择另一个客户端,如 OkHttp 或 Apache Http 客户端,添加对应的 maven 依赖项,然后您可以使用所有设置覆盖客户端。

我选择了 OkHttpClient 并添加到我的AccountSettingsConfig关注 bean:

@Bean
public okhttp3.OkHttpClient okHttpClient() {
    return new OkHttpClient.Builder().hostnameVerifier((s, sslSession) -> true)
            .build();
}
于 2017-08-25T12:46:42.353 回答