feign接口调用对方接口的时候,可能对方的证书什么的有问题,ssl验证不通过,需要关闭ssl校验,记录一下。

    1. import feign.Feign;
    2. import okhttp3.ConnectionPool;
    3. import org.springframework.boot.autoconfigure.AutoConfigureBefore;
    4. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    5. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    7. import org.springframework.cloud.openfeign.FeignAutoConfiguration;
    8. import org.springframework.context.annotation.Bean;
    9. import javax.net.ssl.*;
    10. import java.security.GeneralSecurityException;
    11. import java.security.KeyStore;
    12. import java.security.cert.CertificateException;
    13. import java.security.cert.X509Certificate;
    14. import java.util.concurrent.TimeUnit;
    15. @ConditionalOnProperty(value = "okhttp.client.max-idle-connections")
    16. @ConditionalOnClass(Feign.class)
    17. @AutoConfigureBefore(FeignAutoConfiguration.class)
    18. @EnableConfigurationProperties(OkHttpClientProperties.class)
    19. public class DefaultFeignOkHttpConfig {
    20. @Bean
    21. public okhttp3.OkHttpClient okHttpClient(OkHttpClientProperties okHttpClientProperties) {
    22. X509TrustManager x509TrustManager = systemDefaultTrustManager();
    23. SSLSocketFactory sslSocketFactory = systemDefaultSslSocketFactory(x509TrustManager);
    24. HostnameVerifier notVerify = (hostname, session) -> true;
    25. return new okhttp3.OkHttpClient.Builder()
    26. .sslSocketFactory(sslSocketFactory, x509TrustManager)
    27. .hostnameVerifier(notVerify)
    28. // 设置读超时
    29. .readTimeout(okHttpClientProperties.getReadTimeout(), TimeUnit.SECONDS)
    30. // 设置连接超时
    31. .connectTimeout(okHttpClientProperties.getConnectTimeout(), TimeUnit.SECONDS)
    32. // 设置写超时
    33. .writeTimeout(okHttpClientProperties.getWriteTimeout(), TimeUnit.SECONDS)
    34. // 是否自动重连
    35. .retryOnConnectionFailure(okHttpClientProperties.isRetryOnConnectionFailure())
    36. // 默认创建5个线程,保持5分钟长连接
    37. .connectionPool(new ConnectionPool(okHttpClientProperties.getMaxIdleConnections(),
    38. okHttpClientProperties.getKeepAliveDuration(), TimeUnit.MINUTES))
    39. .build();
    40. }
    41. private X509TrustManager systemDefaultTrustManager() {
    42. try {
    43. TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
    44. TrustManagerFactory.getDefaultAlgorithm());
    45. trustManagerFactory.init((KeyStore) null);
    46. TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    47. trustManagers[0] = new X509TrustManager(){
    48. @Override
    49. public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    50. }
    51. @Override
    52. public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    53. }
    54. @Override
    55. public X509Certificate[] getAcceptedIssuers() {
    56. return new X509Certificate[0];
    57. }
    58. };
    59. return (X509TrustManager) trustManagers[0];
    60. } catch (GeneralSecurityException e) {
    61. // The system has no TLS. Just give up.
    62. throw new AssertionError();
    63. }
    64. }
    65. private SSLSocketFactory systemDefaultSslSocketFactory(X509TrustManager trustManager) {
    66. try {
    67. SSLContext sslContext = SSLContext.getInstance("TLS");
    68. sslContext.init(null, new TrustManager[]{trustManager}, null);
    69. return sslContext.getSocketFactory();
    70. } catch (GeneralSecurityException e) {
    71. // The system has no TLS. Just give up.
    72. throw new AssertionError();
    73. }
    74. }
    75. /**
    76. * fegin远程调用时为请求添加jwt-token的header
    77. *
    78. * @return
    79. */
    80. @Bean
    81. public FeignRequestInterceptor feignRequestInterceptor() {
    82. return new FeignRequestInterceptor();
    83. }
    84. }