JAVA-22600: Changes made for updating the current http client example… (#14473)
This commit is contained in:
parent
de6d040193
commit
684dabd78c
|
@ -1,36 +1,43 @@
|
||||||
package com.baeldung.tlsversion;
|
package com.baeldung.tlsversion;
|
||||||
|
|
||||||
import javax.net.ssl.SSLSocket;
|
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.ssl.SSLContexts;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLSocket;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
|
import org.apache.hc.client5.http.config.TlsConfig;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
|
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
||||||
|
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
|
||||||
|
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
|
import org.apache.hc.core5.http.ssl.TLS;
|
||||||
|
import org.apache.hc.core5.ssl.SSLContexts;
|
||||||
|
import org.apache.hc.core5.util.Timeout;
|
||||||
|
|
||||||
public class ClientTlsVersionExamples {
|
public class ClientTlsVersionExamples {
|
||||||
|
|
||||||
public static CloseableHttpClient setViaSocketFactory() {
|
public static CloseableHttpClient setViaSocketFactory() {
|
||||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
|
||||||
SSLContexts.createDefault(),
|
.setDefaultTlsConfig(TlsConfig.custom()
|
||||||
new String[] { "TLSv1.2", "TLSv1.3" },
|
.setHandshakeTimeout(Timeout.ofSeconds(30))
|
||||||
null,
|
.setSupportedProtocols(TLS.V_1_2, TLS.V_1_3)
|
||||||
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
return HttpClients.custom()
|
||||||
|
.setConnectionManager(cm)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CloseableHttpClient setTlsVersionPerConnection() {
|
public static CloseableHttpClient setTlsVersionPerConnection() {
|
||||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void prepareSocket(SSLSocket socket) {
|
protected void prepareSocket(SSLSocket socket) {
|
||||||
String hostname = socket.getInetAddress().getHostName();
|
String hostname = socket.getInetAddress()
|
||||||
|
.getHostName();
|
||||||
if (hostname.endsWith("internal.system.com")) {
|
if (hostname.endsWith("internal.system.com")) {
|
||||||
socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
|
socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
|
||||||
} else {
|
} else {
|
||||||
|
@ -39,7 +46,14 @@ public class ClientTlsVersionExamples {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
HttpClientConnectionManager connManager = PoolingHttpClientConnectionManagerBuilder.create()
|
||||||
|
.setSSLSocketFactory(sslsf)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return HttpClients.custom()
|
||||||
|
.setConnectionManager(connManager)
|
||||||
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// To configure the TLS versions for the client, set the https.protocols system property during runtime.
|
// To configure the TLS versions for the client, set the https.protocols system property during runtime.
|
||||||
|
@ -47,15 +61,11 @@ public class ClientTlsVersionExamples {
|
||||||
public static CloseableHttpClient setViaSystemProperties() {
|
public static CloseableHttpClient setViaSystemProperties() {
|
||||||
return HttpClients.createSystem();
|
return HttpClients.createSystem();
|
||||||
// Alternatively:
|
// Alternatively:
|
||||||
// return HttpClients.custom().useSystemProperties().build();
|
//return HttpClients.custom().useSystemProperties().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
// Alternatively:
|
try (CloseableHttpClient httpClient = setViaSocketFactory(); CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
|
||||||
// CloseableHttpClient httpClient = setTlsVersionPerConnection();
|
|
||||||
// CloseableHttpClient httpClient = setViaSystemProperties();
|
|
||||||
try (CloseableHttpClient httpClient = setViaSocketFactory();
|
|
||||||
CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
|
|
||||||
|
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.baeldung.tlsversion;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLSocket;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.ssl.SSLContexts;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ClientTlsVersionExamples {
|
||||||
|
|
||||||
|
public static CloseableHttpClient setViaSocketFactory() {
|
||||||
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
||||||
|
SSLContexts.createDefault(),
|
||||||
|
new String[] { "TLSv1.2", "TLSv1.3" },
|
||||||
|
null,
|
||||||
|
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
|
||||||
|
|
||||||
|
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CloseableHttpClient setTlsVersionPerConnection() {
|
||||||
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void prepareSocket(SSLSocket socket) {
|
||||||
|
String hostname = socket.getInetAddress().getHostName();
|
||||||
|
if (hostname.endsWith("internal.system.com")) {
|
||||||
|
socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
|
||||||
|
} else {
|
||||||
|
socket.setEnabledProtocols(new String[] { "TLSv1.3" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
// To configure the TLS versions for the client, set the https.protocols system property during runtime.
|
||||||
|
// For example: java -Dhttps.protocols=TLSv1.1,TLSv1.2,TLSv1.3 -jar webClient.jar
|
||||||
|
public static CloseableHttpClient setViaSystemProperties() {
|
||||||
|
return HttpClients.createSystem();
|
||||||
|
// Alternatively:
|
||||||
|
// return HttpClients.custom().useSystemProperties().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// Alternatively:
|
||||||
|
// CloseableHttpClient httpClient = setTlsVersionPerConnection();
|
||||||
|
// CloseableHttpClient httpClient = setViaSystemProperties();
|
||||||
|
try (CloseableHttpClient httpClient = setViaSocketFactory();
|
||||||
|
CloseableHttpResponse response = httpClient.execute(new HttpGet("https://httpbin.org/"))) {
|
||||||
|
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue