【问题标题】:Disable webclient ssl validation springboot 2.0.4禁用webclient ssl验证springboot 2.0.4
【发布时间】:2021-12-22 05:21:31
【问题描述】:

在 springboot 2.1 上,您可以使用下面的 sn-p 禁用 webclient ssl 验证,但是在 springboot 2.0 上如何做到这一点?其中 ReactorClientHttpConnector 没有这个支持 HttpClient 的构造函数,而是有下面的构造函数

public ReactorClientHttpConnector(Consumer<? super HttpClientOptions.Builder> clientOptions) {
        this.httpClient = HttpClient.create(clientOptions);
    }

以下代码片段适用于 2.1,但不适用于 2.0

SslContext context = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .build();
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(context));
WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient))

【问题讨论】:

    标签: java spring spring-boot ssl spring-webflux


    【解决方案1】:

    API 在 2.0 和 2.1 之间进行了一些演变。在下面找到适用于 2.0.4 的示例:

    package com.example.webclienttest;
    
    import io.netty.handler.ssl.SslContext;
    import io.netty.handler.ssl.SslContextBuilder;
    import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
    import javax.net.ssl.SSLException;
    import org.junit.Test;
    import org.springframework.http.client.reactive.ReactorClientHttpConnector;
    import org.springframework.web.reactive.function.client.WebClient;
    
    public class WebClientTest {
    
      @Test
      public void test() throws SSLException {
        SslContext context = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        WebClient webClient = WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(builder -> builder.sslContext(context)))
            .build();
        String stringMono = webClient.get().uri("https://expired.badssl.com/").retrieve()
            .bodyToMono(String.class).block();
        System.out.println(stringMono);
      }
    }
    

    【讨论】:

    • 谢谢安德烈亚斯,那是救命稻草
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2020-07-12
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2021-04-23
    相关资源
    最近更新 更多