View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.examples;
28  
29  import java.security.cert.X509Certificate;
30  
31  import javax.net.ssl.SSLContext;
32  import javax.net.ssl.SSLSession;
33  
34  import org.apache.hc.client5.http.classic.methods.HttpGet;
35  import org.apache.hc.client5.http.config.TlsConfig;
36  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
37  import org.apache.hc.client5.http.impl.classic.HttpClients;
38  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
39  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
40  import org.apache.hc.client5.http.protocol.HttpClientContext;
41  import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
42  import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
43  import org.apache.hc.core5.http.io.entity.EntityUtils;
44  import org.apache.hc.core5.http.message.StatusLine;
45  import org.apache.hc.core5.http.ssl.TLS;
46  import org.apache.hc.core5.ssl.SSLContexts;
47  import org.apache.hc.core5.util.Timeout;
48  
49  /**
50   * This example demonstrates how to create secure connections with a custom SSL
51   * context.
52   */
53  public class ClientCustomSSL {
54  
55      public final static void main(final String[] args) throws Exception {
56          // Trust standard CA and those trusted by our custom strategy
57          final SSLContext sslcontext = SSLContexts.custom()
58                  .loadTrustMaterial((chain, authType) -> {
59                      final X509Certificate cert = chain[0];
60                      return "CN=httpbin.org".equalsIgnoreCase(cert.getSubjectDN().getName());
61                  })
62                  .build();
63          final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
64                  .setSslContext(sslcontext)
65                  .build();
66          // Allow TLSv1.3 protocol only
67          final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
68                  .setSSLSocketFactory(sslSocketFactory)
69                  .setDefaultTlsConfig(TlsConfig.custom()
70                          .setHandshakeTimeout(Timeout.ofSeconds(30))
71                          .setSupportedProtocols(TLS.V_1_3)
72                          .build())
73                  .build();
74          try (CloseableHttpClient httpclient = HttpClients.custom()
75                  .setConnectionManager(cm)
76                  .build()) {
77  
78              final HttpGet httpget = new HttpGet("https://httpbin.org/");
79  
80              System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
81  
82              final HttpClientContext clientContext = HttpClientContext.create();
83              httpclient.execute(httpget, clientContext, response -> {
84                  System.out.println("----------------------------------------");
85                  System.out.println(httpget + "->" + new StatusLine(response));
86                  EntityUtils.consume(response.getEntity());
87                  final SSLSession sslSession = clientContext.getSSLSession();
88                  if (sslSession != null) {
89                      System.out.println("SSL protocol " + sslSession.getProtocol());
90                      System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
91                  }
92                  return null;
93              });
94          }
95      }
96  
97  }