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

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

上代码。

绕过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);
    }

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

你可能感兴趣的:(Feign绕过Https,SSL验证最简单的两种方式)