BAEL-4448: Added examples for setting TLS version in HttpClient (#9936)
* [BAEL-4448] Added examples for setting TLS version in HttpClient (cherry picked from commit f4d40fc3f3140fd046ed957030e9a54582bd4a67) * [BAEL-4448] Simplified the code for one example * [BAEL-4448] Formatting fixes and moved to new package * [BAEL-4448] Forgot an import and fixed class name typo * [BAEL-4448] Created second module for httpclient and moved article code Co-authored-by: joe <joe.boudreau@pm.me>
This commit is contained in:
parent
ee4fdb9bde
commit
d100adc9c5
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,12 @@
|
|||
## HttpClient 4.x
|
||||
|
||||
This module contains articles about HttpClient 4.x
|
||||
|
||||
### The Course
|
||||
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Set TLS Version in Apache HttpClient](https://www.baeldung.com/TODO)
|
||||
- More articles: [[<-- prev]](../httpclient)
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>httpclient-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>httpclient-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<httpclient.version>4.5.8</httpclient.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,3 +18,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Advanced HttpClient Configuration](https://www.baeldung.com/httpclient-advanced-config)
|
||||
- [HttpClient 4 – Do Not Follow Redirects](https://www.baeldung.com/httpclient-stop-follow-redirect)
|
||||
- [Custom User-Agent in HttpClient 4](https://www.baeldung.com/httpclient-user-agent-header)
|
||||
- More articles: [[next -->]](../httpclient-2)
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -424,6 +424,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-2</module>
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
|
@ -935,6 +936,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-2</module>
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
|
|
Loading…
Reference in New Issue