forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-4448: Added examples for setting TLS version in HttpClient (euge…
…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
1 parent
10d5278
commit 38f35c4
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
64 changes: 64 additions & 0 deletions
64
httpclient-2/src/main/java/com/baeldung/tlsversion/ClientTlsVersionExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters