如何从 spring-cloud-netflix-feign 在 Feign-Client 中设置 HostnameVerifier
How to set HostnameVerifier in Feign-Client from spring-cloud-netflix-feign
我正在尝试设置我的 Spring Cloud Feign 客户端以使用自定义 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());
}
}
默认情况下会创建一个带有 HttpURLConnection 的 LoadBalancerFeignClient,并且无法覆盖它的 HostnameVerifier 设置。为了覆盖它,选择另一个客户端,如 OkHttp 或 Apache Http Client,添加对应的 maven 依赖项,然后您可以使用所有设置覆盖客户端。
我选择了 OkHttpClient 并添加到我的 AccountSettingsConfig
follow bean:
@Bean
public okhttp3.OkHttpClient okHttpClient() {
return new OkHttpClient.Builder().hostnameVerifier((s, sslSession) -> true)
.build();
}
我正在尝试设置我的 Spring Cloud Feign 客户端以使用自定义 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());
}
}
默认情况下会创建一个带有 HttpURLConnection 的 LoadBalancerFeignClient,并且无法覆盖它的 HostnameVerifier 设置。为了覆盖它,选择另一个客户端,如 OkHttp 或 Apache Http Client,添加对应的 maven 依赖项,然后您可以使用所有设置覆盖客户端。
我选择了 OkHttpClient 并添加到我的 AccountSettingsConfig
follow bean:
@Bean
public okhttp3.OkHttpClient okHttpClient() {
return new OkHttpClient.Builder().hostnameVerifier((s, sslSession) -> true)
.build();
}