Feign忽略Https的SSL最佳方案(且保证负载均衡将失效)

  • 同时解决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 否则负载均衡将失效(现象:消费者无法从注册中心获取服务提供者的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);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微服务之间的调用,如果需要进行负载均衡,可以使用Ribbon和Feign的结合。同样需要在Feign的配置中启用HTTPS支持,并且配置Ribbon的负载均衡策略。具体步骤如下: 1. 引入Ribbon和Feign的依赖 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. 配置Feign和Ribbon的HTTPS支持和负载均衡策略 ``` @Configuration public class FeignConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Client feignClient() { return new Client.Default(getSSLSocketFactory(), null); } private SSLSocketFactory getSSLSocketFactory() { try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }, new SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception e) { throw new RuntimeException(e); } } @Bean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(messageConverters)); } @Bean public IRule ribbonRule() { return new RandomRule(); } @Bean public IPing ribbonPing() { return new PingUrl(false, "/health"); } @Bean public ServerList<Server> ribbonServerList() { return new ConfigurationBasedServerList(); } @Bean public OkHttpClient ribbonOkHttpClient() { return new OkHttpClient.Builder().sslSocketFactory(getSSLSocketFactory(), new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }).build(); } @Bean public RibbonClientConfiguration ribbonClientConfiguration(IClientConfig config, ILoadBalancer loadBalancer, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, RetryHandler retryHandler, OkHttpClient ribbonOkHttpClient) { return new RibbonClientConfiguration(config, loadBalancer, serverList, serverListFilter, rule, ping, retryHandler, ribbonOkHttpClient); } } ``` 其中,`ribbonRule`方法配置了Ribbon的负载均衡策略,这里采用了随机策略,可以根据实际需求进行调整。 3. 在Feign的接口中使用`https`协议和负载均衡地址 ``` @FeignClient(name = "example", configuration = ExampleConfig.class) public interface ExampleClient { @GetMapping("/api") String get(); } ``` 其中,`configuration`参数指定了配置类,用于配置Ribbon相关的设置。 4. 在配置类中配置Ribbon的信息 ``` @Configuration public class ExampleConfig { @Bean public IRule ribbonRule() { return new RandomRule(); } @Bean public IPing ribbonPing() { return new PingUrl(false, "/health"); } @Bean public ServerList<Server> ribbonServerList(IClientConfig config) { return new ConfigurationBasedServerList(); } } ``` 其中,`ribbonRule`方法和`ribbonPing`方法分别配置了Ribbon的负载均衡策略和健康检查的地址,`ribbonServerList`方法返回一个`ServerList`对象,用于配置可用的服务列表。 5. 在微服务中使用Feign进行调用 ``` @RestController public class ExampleController { @Autowired private ExampleClient exampleClient; @GetMapping("/example") public String get() { return exampleClient.get(); } } ``` 这样就可以在微服务中使用Feign调用HTTPS接口,并且进行负载均衡了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值