openfeign 绕开ssl
时间: 2023-10-12 13:56:35 浏览: 134
要绕开OpenFeign的SSL验证,可以通过配置FeignClient的属性来实现。在FeignClient的注解中,可以添加`@FeignClient(name = "example", configuration = ExampleFeignConfiguration.class)`,其中`ExampleFeignConfiguration`是一个自定义的配置类。在这个配置类中,可以使用`feign.okhttp.OkHttpClient`来创建一个自定义的OkHttpClient,并通过设置`sslSocketFactory`和`hostnameVerifier`来绕开SSL验证。
以下是一个示例的配置类:
```java
@Configuration
public class ExampleFeignConfiguration {
@Bean
public OkHttpClient okHttpClient() throws Exception {
TrustManager\[\] trustAllCerts = new TrustManager\[\]{new X509TrustManager() {
public X509Certificate\[\] getAcceptedIssuers() {
return new X509Certificate\[0\];
}
public void checkClientTrusted(X509Certificate\[\] certs, String authType) {
}
public void checkServerTrusted(X509Certificate\[\] certs, String authType) {
}
}};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
HostnameVerifier hostnameVerifier = (hostname, session) -> true;
return new OkHttpClient.Builder()
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts\[0\])
.hostnameVerifier(hostnameVerifier)
.build();
}
}
```
在这个配置类中,我们创建了一个自定义的OkHttpClient,通过设置`sslSocketFactory`和`hostnameVerifier`来绕开SSL验证。然后,在FeignClient的注解中,指定使用这个配置类。
这样配置之后,OpenFeign就会使用自定义的OkHttpClient来发送请求,从而绕开SSL验证。
#### 引用[.reference_title]
- *1* *3* [OpenFeign 微服务之间调用 https ssl证书配置](https://blog.csdn.net/qq_35414397/article/details/108322471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Feign忽略https接口调用SSL证书验证](https://blog.csdn.net/qq_36599564/article/details/116176888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文