Feign绕过Https,SSL验证最简单的两种方式

时间:2021-6-7 作者:qvyue

既然是绕过那肯定是不存在证书的啦

上代码。

绕过SSL的核心bean

    @Bean
    public Client skipSSLClient() {
        try {
            return Optional.of(
                    new SSLContextBuilder()
                            .loadTrustMaterial(null, (chain, authType) -> true)
                            .build()
            ).map(s ->
                    new Client.Default(
                            s.getSocketFactory(),
                            new NoopHostnameVerifier())
            ).get();
        } catch (Exception e) {
            log.error("Create feign client with SSL config failed", e);
            return new Client.Default(null, null);
        }
    }

第一种方式自己构建Feign Api,将我们的skipSSLClient设置到 Feign中就完事了。此时的URL已经是 https://xxx

/**
 * @ClassName PerformanceDataHandle
 * @Author Lijw
 * @Date 2021/2/20 15:59
 * @Description
 **/
@Slf4j
@Component
@Import(FeignClientsConfiguration.class)
public class PerformanceDataHandle {

    @Autowired
    Decoder decoder;
    @Autowired
    Encoder encoder;
    @Autowired
    Client skipSSLClient;


    public VbngPerformanceApi getFeign(String url) {
        return Feign.builder()
                .client(skipSSLClient)
                .encoder(encoder).decoder(decoder)
                .options(new Request.Options(60000, 60000))
                .retryer(new Retryer.Default(5000, 5000, 3))
                .logLevel(Logger.Level.BASIC)
                .target(VbngPerformanceApi.class, url);
    }

}

2.第二种方式直接注入到,Feign.Builer Bean中就能使用了。

    @Autowired
    Client skipSSLClient;

 @Bean
    public Feign.Builder client() {
        return Feign.builder().client(skipSSLClient);
    }

是不是很简单,欢迎提出建议✨

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。