问题
Below is the old code which I am trying to replace. How can I use webclient to follow same behavior? If anyone can guide me on this it will be great.
I check for code to use HostnameVerifier but i did not find any link which can help me.
public void initSSL() {
if (logger.isDebugEnabled()) {
logger.debug("HttpBasedUrlConnection - Initializing SSL");
}
// Configure truststore location for everybody
final String homePath = getHomePath();
synchronized (this) {
trustStorePath = homePath + "/conf/cert/client.keystore";
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
}
// HEADSUP: This violates HTTPS host verification, since we arnt setting up SSL certs right.
final HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(final String urlHostName, final SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// Prepare SSL socket factory with all available certificates
loadCertificates();
}
I came across StackOverflow link which suggests how to accept all the certificates. That is not exactly what I need but I can use it as temp solution until i find proper one.
public static WebClient getWebClient() throws SSLException {
String baseUrl = "https://localhost:3454/stackoverflow";
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(httpClient);
return WebClient.builder().clientConnector(httpConnector).baseUrl(baseUrl).build();
}
I tried it but getting the following error
Runtime exception: 'long io.netty.util.internal.PlatformDependent.objectFieldOffset(java.lang.reflect.Field)' java.lang.NoSuchMethodError: 'long io.netty.util.internal.PlatformDependent.objectFieldOffset(java.lang.reflect.Field)'
at io.netty.channel.nio.NioEventLoop$4.run(NioEventLoop.java:213) ~[netty-transport.jar:4.1.34.Final]
at java.base/java.security.AccessController.doPrivileged(Native Method) ~[?:?]
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:203) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:143) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:127) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:36) ~[netty-transport.jar:4.1.34.Final]
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84) ~[jboss-client.jar:16.0.0.Final]
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58) ~[jboss-client.jar:16.0.0.Final]
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47) ~[jboss-client.jar:16.0.0.Final]
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:59) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:77) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:72) ~[netty-transport.jar:4.1.34.Final]
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:59) ~[netty-transport.jar:4.1.34.Final]
at reactor.netty.resources.DefaultLoopResources.cacheNioServerLoops(DefaultLoopResources.java:171) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.resources.DefaultLoopResources.cacheNioClientLoops(DefaultLoopResources.java:191) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.resources.DefaultLoopResources.onClient(DefaultLoopResources.java:185) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpResources.onClient(TcpResources.java:168) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.http.client.HttpClientConnect$HttpTcpClient.connect(HttpClientConnect.java:143) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpClientOperator.connect(TcpClientOperator.java:43) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpClientOperator.connect(TcpClientOperator.java:43) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpClientOperator.connect(TcpClientOperator.java:43) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpClientOperator.connect(TcpClientOperator.java:43) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.tcp.TcpClient.connect(TcpClient.java:186) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.http.client.HttpClientFinalizer.connect(HttpClientFinalizer.java:68) ~[reactor-netty.jar:0.8.6.RELEASE]
at reactor.netty.http.client.HttpClientFinalizer.responseConnection(HttpClientFinalizer.java:85) ~[reactor-netty.jar:0.8.6.RELEASE]
at org.springframework.http.client.reactive.ReactorClientHttpConnector.connect(ReactorClientHttpConnector.java:111) ~[spring-web.jar:5.1.6.RELEASE]
at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.exchange(ExchangeFunctions.java:103) ~[spring-webflux.jar:5.1.6.RELEASE]
at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.exchange(DefaultWebClient.java:319) ~[spring-webflux.jar:5.1.6.RELEASE]
at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.retrieve(DefaultWebClient.java:368) ~[spring-webflux.jar:5.1.6.RELEASE]
来源:https://stackoverflow.com/questions/61773397/spring-webclient-ssl-configuration