java https双向认证证书

// 双向认证证书
KeyStore keyStore = KeyStore.getInstance(“PKCS12”);
KeyStore trustStore = KeyStore.getInstance(“jks”);
// keyStore是服务端验证客户端的证书,trustStore是客户端的信任证书
InputStream ksIn = new FileInputStream(“E:/Java/jre8/lib/security/re/1.pfx”);
InputStream tsIn = new FileInputStream(new File(“E:/Java/jre8/lib/security/re/1”));

    keyStore.load(ksIn, "123456".toCharArray());

    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
            .loadKeyMaterial(keyStore, "123456".toCharArray()).setSecureRandom(new SecureRandom()).useSSL().build();

    ConnectionSocketFactory pSocketFactory = new PlainConnectionSocketFactory();
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext);

    Registry r = RegistryBuilder. create()
            .register("http", pSocketFactory).register("https", sslConnectionSocketFactory).build();
    PoolingHttpClientConnectionManager secureConnectionManager = new PoolingHttpClientConnectionManager(r);

    HttpClientBuilder secureHttpBulder = HttpClients.custom().setConnectionManager(secureConnectionManager);
    HttpClient client = secureHttpBulder.build();

    HttpGet httpGet = new HttpGet("https://xxx.com");
    HttpResponse httpResponse1 = client.execute(httpGet);

你可能感兴趣的:(JAVA)