HttpClient SSLContexts

使用SSLContext,您可以在客户端和服务器之间建立安全连接。它有助于保护敏感信息,例如信用卡号、用户名、密码、PIN 码等。

您可以通过使用HttpClient库创建自己的 SSL 上下文来使连接更加安全。

按照下面给出的步骤使用 HttpClient 库自定义 SSLContext

1)创建 SSLContextBuilder 对象

SSLContextBuilder是 SSLContext 对象的构建器。使用SSLContexts类的custom()方法创建其对象。

//Creating SSLContextBuilder object
SSLContextBuilder SSLBuilder = SSLContexts.custom();

2)加载密钥库

在路径Java_home_directory/jre/lib/security/ 中,可以找到一个名为cacerts.conf的文件。将此保存为您的密钥存储文件(扩展名为 .jks)。使用SSLContextBuilder类的loadTrustMaterial()方法加载密钥库文件及其密码(默认情况下为changeit)。

//Loading the Keystore file
File file = new File("mykeystore.jks");
SSLBuilder = SSLBuilder.loadTrustMaterial(file, "changeit".toCharArray());

3)构建 SSLContext 对象

SSLContext 对象表示安全套接字协议实现。使用build()方法构建 SSLContext 。

//Building the SSLContext
SSLContext sslContext = SSLBuilder.build();

4)创建 SSLConnectionSocketFactory 对象

SSLConnectionSocketFactory是用于 TSL 和 SSL 连接的分层套接字工厂。使用它,您可以使用受信任的证书列表验证 Https 服务器并验证给定的 Https 服务器。

您可以通过多种方式创建它。根据您创建SSLConnectionSocketFactory对象的方式,您可以允许所有主机、仅允许自签名证书、仅允许特定协议等。

要仅允许特定协议,请通过将 SSLContext 对象、表示需要支持的协议的字符串数组、表示需要支持的密码套装的字符串数组以及 HostnameVerifier 对象传递给其构造函数来创建SSLConnectionSocketFactory对象。

new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,    
   SSLConnectionSocketFactory.getDefaultHostnameVerifier());

要允许所有主机,请通过传递 SSLContext 对象和NoopH​​ostnameVerifier对象来创建SSLConnectionSocketFactory对象。

//Creating SSLConnectionSocketFactory SSLConnectionSocketFactory object
SSLConnectionSocketFactory sslConSocFactory = new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier());

5)创建一个 HttpClientBuilder 对象

使用HttpClients类的custom()方法创建一个 HttpClientBuilder 对象。

//Creating HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();

6)设置 SSLConnectionSocketFactory 对象

使用setSSLSocketFactory()方法将 SSLConnectionSocketFactory 对象设置为HttpClientBuilder。

//Setting the SSLConnectionSocketFactory
clientbuilder = clientbuilder.setSSLSocketFactory(sslConSocFactory);

7)构建 CloseableHttpClient 对象

通过调用build()方法构建CloseableHttpClient对象。

//Building the CloseableHttpClient
CloseableHttpClient httpclient = clientbuilder.build();

8)创建一个 HttpGet 对象

上述HttpGet类表示检索使用URI给定服务器的信息的HTTP GET请求。

通过传递表示 URI 的字符串来实例化 HttpGet 类,从而创建 HTTP GET 请求。

//Creating the HttpGet request
HttpGet httpget = new HttpGet("http://www.yiidian.com/");

9)执行请求

使用execute()方法执行请求。

//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget);

HttpClient SSLContexts的完整例子

以下示例演示了自定义的 SSLContrext 

package com.yiidian;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.File;

public class ClientCustomSSL {
   
   public final static void main(String[] args) throws Exception {

      //Creating SSLContextBuilder object
      SSLContextBuilder SSLBuilder = SSLContexts.custom();
  
      //Loading the Keystore file
      File file = new File("mykeystore.jks");
      SSLBuilder = SSLBuilder.loadTrustMaterial(file,
         "changeit".toCharArray());

      //Building the SSLContext usiong the build() method
      SSLContext sslcontext = SSLBuilder.build();
 
      //Creating SSLConnectionSocketFactory object
      SSLConnectionSocketFactory sslConSocFactory = new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier());
 
      //Creating HttpClientBuilder
      HttpClientBuilder clientbuilder = HttpClients.custom();

      //Setting the SSLConnectionSocketFactory
      clientbuilder = clientbuilder.setSSLSocketFactory(sslConSocFactory);

      //Building the CloseableHttpClient
      CloseableHttpClient httpclient = clientbuilder.build();
      
      //Creating the HttpGet request
      HttpGet httpget = new HttpGet("https://www.yiidian.com/");
 
      //Executing the request
      HttpResponse httpresponse = httpclient.execute(httpget);

      //printing the status line
      System.out.println(httpresponse.getStatusLine());

      //Retrieving the HttpEntity and displaying the no.of bytes read
      HttpEntity entity = httpresponse.getEntity();
      if (entity != null) {
         System.out.println(EntityUtils.toByteArray(entity).length);
      } 
   }
}

输出结果为:

HTTP/1.1 200 OK
1270

 

热门文章

优秀文章