• feign中通过https请求绕过SSL证书验证


    在FeignConfig配置类中添加如下内容:

    package cn.com.rivercloud.common;
    
    import feign.Client;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
    import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import feign.Logger;
    
    import javax.net.ssl.*;
    
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
    import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
    import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
    
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    
    @Configuration
    public class FeignConfiguration {
        /**
         * 日志级别
         *
         * @return
         */
        //@Bean
        //Logger.Level feignLoggerLevel() {
        //    return Logger.Level.FULL;
        //}
        
    
        @Bean
        @ConditionalOnMissingBean
        public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
                                  SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyManagementException {
            SSLContext ctx = SSLContext.getInstance("SSL");
            X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
    
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
    
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            return new LoadBalancerFeignClient(new Client.Default(ctx.getSocketFactory(),
                    new HostnameVerifier() {
    
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            // TODO Auto-generated method stub
                            return true;
                        }
                    }),
                    cachingFactory, clientFactory);
        }
    
    
    }

    其中,依赖为:

    <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </dependency>
                <!-- 使用Apache HttpClient替换Feign原生httpclient -->
                <dependency>
                    <!--不是netfix包下了,应该是独立出来开源了-->
                    <groupId>io.github.openfeign</groupId>
                    <artifactId>feign-httpclient</artifactId>
                    <version>9.4.0</version>
                    <!-- <version>9.5.1</version>   这里可以不指定version spring-boot-parent中已经有版本定义-->
                </dependency>
                
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>

    有参考,若有侵权请告知,立马就删除

  • 相关阅读:
    Android开源项目收藏
    ubuntu修改ip获取方式(静态,动态)
    linux内核调用用户空间程序
    linux 改变目录下所有文件及其子文件夹下的权限
    jquery remove() empty()
    jquery之attr()和removeAttr() prop的使用场所
    jquery 获取对象的八种总结
    html子标签浮动父标签无法扩充
    静态代码块
    java static介绍
  • 原文地址:https://www.cnblogs.com/notchangeworld/p/12667268.html
Copyright © 2020-2023  润新知