{"id":81802,"date":"2020-09-24T16:51:48","date_gmt":"2020-09-24T16:51:48","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=87638"},"modified":"2020-09-24T16:51:48","modified_gmt":"2020-09-24T16:51:48","slug":"how-to-set-tls-version-in-apache-httpclient","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/09\/24\/how-to-set-tls-version-in-apache-httpclient\/","title":{"rendered":"How to Set TLS Version in Apache HttpClient"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"introduction\">1. Introduction<\/h2>\n<div class=\"bd-anchor\" id=\"introduction\"><\/div>\n<p>Apache HttpClient is a low-level, lightweight client-side HTTP library for communicating with HTTP servers. In this tutorial, we&#8217;ll learn how to configure the supported Transport Layer Security (TLS) version(s) when using <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/httpclient-guide\">HttpClient<\/a>. We&#8217;ll begin with an overview of how TLS version negotiation works between a client and a server. Afterward, we&#8217;ll look at <strong>three different ways of configuring the supported TLS versions when using HttpClient<\/strong>.<\/p>\n<h2 data-id=\"tls-version-negotiation\">2. TLS Version Negotiation<\/h2>\n<div class=\"bd-anchor\" id=\"tls-version-negotiation\"><\/div>\n<p>TLS is an internet protocol that provides secure, trusted communication between two parties. It encapsulates application layer protocols like HTTP. The TLS protocol has been revised several times since it was first published in 1999. <strong>Therefore, it&#8217;s important for the client and server to first agree on which version of TLS they will use when establishing a new connection.<\/strong> The TLS version is agreed on after the client and server exchange hello messages:<\/p>\n<ol>\n<li>The client sends a list of supported TLS versions.<\/li>\n<li>The server chooses one and includes the selected version in the response.<\/li>\n<li>The client and server continue the connection setup using the selected version.<\/li>\n<\/ol>\n<p>It&#8217;s important to correctly configure the supported TLS versions of a web client because of the risk of a <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/en.wikipedia.org\/wiki\/Downgrade_attack\">downgrade attack<\/a>.<strong> Note that in order to use the latest version of TLS (TLS 1.3), we must be using Java 11 or later.<\/strong><\/p>\n<h2 data-id=\"setting-the-tls-version-statically\">3. Setting the TLS Version Statically<\/h2>\n<div class=\"bd-anchor\" id=\"setting-the-tls-version-statically\"><\/div>\n<h3 data-id=\"1-sslconnectionsocketfactory\">3.1. <em>SSLConnectionSocketFactory<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"1-sslconnectionsocketfactory\"><\/div>\n<p>Let&#8217;s use the <em>HttpClientBuilder<\/em> exposed by the <em>HttpClients#custom<\/em> builder method in order to customize our <em>HTTPClient<\/em> configuration. This builder pattern allows us to pass in our own <em>SSLConnectionSocketFactory<\/em>, which will be instantiated with the desired set of supported TLS versions:<\/p>\n<pre><code class=\"language-java\">SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(\r\n  SSLContexts.createDefault(),\r\n  new String[] { \"TLSv1.2\", \"TLSv1.3\" },\r\n  null,\r\n  SSLConnectionSocketFactory.getDefaultHostnameVerifier());\r\nCloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();<\/code><\/pre>\n<p>The returned <em>Httpclient<\/em> object can now execute HTTP requests. By setting the supported protocols explicitly in the <em>SSLConnectionSocketFactory <\/em>constructor, the client will only support communication over TLS 1.2 or TLS 1.3. Note that in Apache HttpClient versions prior to 4.3, the class was called <em>SSLSocketFactory<\/em>.<\/p>\n<h3 data-id=\"2-java-runtime-argument\">3.2. Java Runtime Argument<\/h3>\n<div class=\"bd-anchor\" id=\"2-java-runtime-argument\"><\/div>\n<p>Alternatively, we can configure the supported TLS versions using Java&#8217;s <em>https.protocols<\/em> system property. This method prevents having to hard-code values into the application code. Instead, we&#8217;ll configure the <em>HttpClient<\/em> to use the system properties when setting up connections. The HttpClient API provides two ways to do this. The first is via <em>HttpClients#createSystem<\/em>:<\/p>\n<pre><code class=\"language-java\">CloseableHttpClient httpClient = HttpClients.createSystem();<\/code><\/pre>\n<p>If more client configuration is required, we can use the builder method instead:<\/p>\n<pre><code class=\"language-java\">CloseableHttpClient httpClient = HttpClients.custom().useSystemProperties().build();<\/code><\/pre>\n<p>Both methods tell <em>HttpClient<\/em> to use system properties during connection configuration. This allows us to set the required TLS versions with a command-line argument during application runtime. For example:<\/p>\n<pre><code class=\"language-bash\">$ java -Dhttps.protocols=TLSv1.1,TLSv1.2,TLSv1.3 -jar webClient.jar<\/code><\/pre>\n<h2 data-id=\"setting-the-tls-version-dynamically\">4. Setting the TLS Version Dynamically<\/h2>\n<div class=\"bd-anchor\" id=\"setting-the-tls-version-dynamically\"><\/div>\n<p>It&#8217;s also possible to set the TLS version based on connection details such as hostname and port. We&#8217;ll extend the <em>SSLConnectionSocketFactory<\/em> and override the <em>prepareSocket<\/em> method. The client calls the <em>prepareSocket<\/em> method before it initiates a new connection. <strong>This will let us decide which TLS protocols to use on a per-connection basis.<\/strong> It&#8217;s also possible to enable support for older TLS versions, but only if the remote host has a specific subdomain:<\/p>\n<pre><code class=\"language-java\">SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContexts.createDefault()){\r\n    @Override\r\n    protected void prepareSocket(SSLSocket socket) {\r\n        String hostname = socket.getInetAddress().getHostName();\r\n        if (hostname.endsWith(\"internal.system.com\")){\r\n            socket.setEnabledProtocols(new String[] { \"TLSv1\", \"TLSv1.1\", \"TLSv1.2\", \"TLSv1.3\" });\r\n        }\r\n        else {\r\n            socket.setEnabledProtocols(new String[] {\"TLSv1.3\"});\r\n        }\r\n    }\r\n};\r\n<br>\r\nCloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();\r\n<\/code><\/pre>\n<p>In the example above, the <em>prepareSocket<\/em> method first gets the remote hostname that the <em>SSLSocket<\/em> will connect to. <strong>The hostname is then used to determine with TLS protocols to enable.<\/strong> Now, our HTTP Client will enforce TLS 1.3 on every request except if the destination hostname is of the form *<em>.internal.example.com.<\/em> With the ability to insert custom logic before the creation of a new <em>SSLSocket<\/em>, our application can now customize the TLS communication details.<\/p>\n<h2 data-id=\"conclusion\">5. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>In this article, we looked at three different ways of configuring the supported TLS versions when using the Apache HttpClient library. We&#8217;ve seen how the TLS versions can be set for all connections, or on a per-connection basis. The code samples used in this article are available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/httpclient-2\">over on GitHub<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/apache-httpclient-tls\/\" >How to Set TLS Version in Apache HttpClient<\/a> first appeared on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/\" >Baeldung<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/635926406\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/635926406\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/apache-httpclient-tls#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/apache-httpclient-tls\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn how to set the TLS version in Apache HTTPClient<\/p>\n<p>The post <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/feeds.feedblitz.com\/~\/635926406\/0\/baeldung~How-to-Set-TLS-Version-in-Apache-HttpClient\/\" target=\"_blank\">How to Set TLS Version in Apache HttpClient<\/a> first appeared on <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/www.baeldung.com\/\" target=\"_blank\">Baeldung<\/a>.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/635926406\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/635926406\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/apache-httpclient-tls#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/apache-httpclient-tls\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":214,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"How to Set TLS Version in Apache HttpClient - ITTeacherITFreelance.hk","description":"Learn how to set the TLS version in Apache HTTPClient The post How to Set TLS Version in Apache HttpClient first appeared on Baeldung . &nbsp; &nbsp; &nbsp; &nb"},"footnotes":""},"categories":[6],"tags":[5440],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/81802"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=81802"}],"version-history":[{"count":7,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/81802\/revisions"}],"predecessor-version":[{"id":85416,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/81802\/revisions\/85416"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=81802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=81802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=81802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}