Skip to content

Commit

Permalink
BAEL-4448: Added examples for setting TLS version in HttpClient (euge…
Browse files Browse the repository at this point in the history
…np#9936)

* [BAEL-4448] Added examples for setting TLS version in HttpClient

(cherry picked from commit f4d40fc)

* [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 <[email protected]>
  • Loading branch information
joe-boudreau and joe authored Sep 24, 2020
1 parent 10d5278 commit 38f35c4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
13 changes: 13 additions & 0 deletions httpclient-2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.class

#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*

# Packaged files #
*.jar
*.war
*.ear
12 changes: 12 additions & 0 deletions httpclient-2/README.md
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions httpclient-2/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions httpclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
<module>hazelcast</module>
<module>helidon</module>
<module>httpclient</module>
<module>httpclient-2</module>
<module>httpclient-simple</module>
<module>hystrix</module>

Expand Down Expand Up @@ -935,6 +936,7 @@
<module>hazelcast</module>
<module>helidon</module>
<module>httpclient</module>
<module>httpclient-2</module>
<module>httpclient-simple</module>
<module>hystrix</module>

Expand Down

0 comments on commit 38f35c4

Please sign in to comment.