美文网首页
Feign系列 - 绕过SSL验证的方案

Feign系列 - 绕过SSL验证的方案

作者: 八月狮子丶 | 来源:发表于2021-08-13 04:58 被阅读0次

    Feign系列 - 绕过SSL验证的方案

    背景

    做一个项目的时候,需要调用https的接口,但是对方的ssl证书已经过期,而Feign默认会进行SSL认证,导致接口调用有点问题。

    解决方案这里记录下。

    Maven依赖

    • Spring Boot:2.2.8.RELEASE
    • Spring Cloud:Hoxton.SR8
    <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>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
    </dependency>
    

    代码

    创建Feign的配置类

    import feign.Client;
    import feign.codec.Encoder;
    import feign.form.FormEncoder;
    import org.springframework.beans.factory.ObjectFactory;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
    import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
    import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
    import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
    import org.springframework.cloud.openfeign.support.SpringEncoder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.X509Certificate;
    
    @Configuration
    public class FeignConfiguration {
    
        @Bean
        public CachingSpringLoadBalancerFactory cachingFactory(SpringClientFactory clientFactory) {
            return new CachingSpringLoadBalancerFactory(clientFactory);
        }
    
        @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) {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            return new LoadBalancerFeignClient(new Client.Default(ctx.getSocketFactory(),
                    (hostname, session) -> true),
                    cachingFactory, clientFactory);
        }
    }
    

    Feign接口

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.PostMapping;
    
    import java.util.Map;
    
    
    @FeignClient(name = "ignoreSSLFeign", url="https://127.0.0.1:8080", configuration = FeignConfiguration.class)
    public interface IgnoreSSLFeign {
    
        @PostMapping(value = "/ignore/ssl")
        Object test(TestParam param);
    
    }
    

    添加EnableFeignClients使FeignClient注解生效

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @EnableFeignClients
    @SpringBootApplication
    public class AppRun {
    
        public static void main(String[] args) {
            SpringApplication.run(AppRun.class, args);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Feign系列 - 绕过SSL验证的方案

          本文链接:https://www.haomeiwen.com/subject/vbtdbltx.html