gpt4 book ai didi

java - 禁用webclient ssl验证springboot 2.0.4

转载 作者:行者123 更新时间:2023-12-04 22:39:51 26 4
gpt4 key购买 nike

在 springboot 2.1 上,您可以使用下面的代码段禁用 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))

最佳答案

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

关于java - 禁用webclient ssl验证springboot 2.0.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69899232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com