JDK1.6 and Http-component3.1 支持 TLS1.2

業務需求:舊項目須要支持TLS1.2訪問外部,主要是如下兩點要求java

1.項目使用JDK1.6 需三方TLS1.2支持git

2.項目使用Apache-httpCcomponent3.1,需貼合現有開發者習慣,防止他們撲街.github

網上目前沒找到現成的方案,本身搞定了.apache

解決方法以下:socket

1.獲得支持TLS1.2的Socket工廠:ide

https://github.com/a--i--r/TLSSocketFactory加密

直接使用便可,依賴的是BouncyCastle三方加密庫,感謝日本友人spa

2.將該工廠封裝以下:.net

package my;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import third.tls.TLSSocket;
import third.tls.TLSSocketFactory;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 代理一個工廠
 * @author xing.yang01@hand-china.com
 */
public class TLS12ProtocolSocketFactory implements ProtocolSocketFactory {
    public static final TLSSocketFactory INNER_SOCKET_FACTORY = new TLSSocketFactory(true,6000);

    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException {
        Socket socket = INNER_SOCKET_FACTORY.createSocket(host, port, localAddress, localPort);
        //重點,這裏手動handshake一下,重點 by xing.yang01@hand-china.com
        ((TLSSocket)socket).startHandshake();
        return socket;
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
        Socket socket = INNER_SOCKET_FACTORY.createSocket(host, port, localAddress, localPort);
        ((TLSSocket)socket).startHandshake();
        return socket;
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        Socket socket = INNER_SOCKET_FACTORY.createSocket(host, port);
        ((TLSSocket)socket).startHandshake();
        return socket;
    }
}

3.使用方法代理

import my.TLS12ProtocolSocketFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;

import java.io.IOException;

public class Test {

    public static void main(String... args) throws IOException {

        TLS12ProtocolSocketFactory tls12ProtocolSocketFactory = new TLS12ProtocolSocketFactory();
        Protocol.registerProtocol("https", new Protocol("https", tls12ProtocolSocketFactory, 443));

        HttpClient httpClient=new HttpClient();

        HttpMethod httpMethod=new PostMethod("https://www.baidu.com");
        HttpMethodParams httpParams=new HttpMethodParams();
        httpMethod.setParams(httpParams);

        try {
            httpClient.executeMethod(httpMethod);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String responseBodyAsString = httpMethod.getResponseBodyAsString();
    }
}

 

問題就解決了

相關文章
相關標籤/搜索