- 同時解決Https的SSL證書驗證問題和feign不支持Patch請求方法的問題
代碼 1. 工具類 OkHttpUtils.java
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
/**
* @author Vania
*/
public class OkHttpUtils {
/**
* X509TrustManager instance which ignored SSL certification
*/
public static final X509TrustManager IGNORE_SSL_TRUST_MANAGER_X509 = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
};
/**
* Get initialized SSLContext instance which ignored SSL certification
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static SSLContext getIgnoreInitedSslContext() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{IGNORE_SSL_TRUST_MANAGER_X509}, new SecureRandom());
return sslContext;
}
/**
* Get HostnameVerifier which ignored SSL certification
*
* @return
*/
public static HostnameVerifier getIgnoreSslHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
}
}
代碼 2. 工具類 FeignConfiguration.java
import feign.Client;
import feign.okhttp.OkHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
@Slf4j
@Configuration
public class FeignConfiguration {
/**
* 解決 feign client 中https不安全的問題
*
* @param cachingFactory
* @param clientFactory
* @return
*/
@Bean
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
// 此處必須為 new LoadBalancerFeignClient 否則負載均衡將失效(現(xiàn)象:消費者無法從注冊中心獲取服務(wù)提供者的ip)
// 這個只能解決忽略https證書驗證
// return new LoadBalancerFeignClient(new Client.Default(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build().getSocketFactory(), new NoopHostnameVerifier()),
// cachingFactory, clientFactory);
// 使用okhttp 解決證書驗證 和 Patch請求方法不支持的問題
return new LoadBalancerFeignClient(new OkHttpClient(new okhttp3.OkHttpClient()
.newBuilder()
.sslSocketFactory(OkHttpUtils.getIgnoreInitedSslContext().getSocketFactory(), OkHttpUtils.IGNORE_SSL_TRUST_MANAGER_X509)
.hostnameVerifier(OkHttpUtils.getIgnoreSslHostnameVerifier())
.build()),
cachingFactory, clientFactory);
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-647177.html
文章來源:http://www.zghlxwxcb.cn/news/detail-647177.html
到了這里,關(guān)于Feign忽略Https的SSL最佳方案(且保證負載均衡將失效)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!