简体   繁体   English

如何使用 https 客户端和 ssl 证书从 java 调用 apis

[英]how to invoke apis form java using https client with ssl certificate

I am invoking rest api from java file using HttpClient.我正在使用 HttpClient 从 java 文件调用 rest api。 By using that I am able to call http api but not https api.通过使用它,我可以调用 http api 但不能调用 https Z8A5DA52ED126447D359E70AAZ.A

I am getting below error, while calling httpsapi.我在调用 httpsapi 时遇到错误。

**javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)**

I want to call https api using CloseableHttpClient.我想使用 CloseableHttpClient 调用 https api。

I am having certificate file that is having private key.我有具有私钥的证书文件。 Please let me know how I can use this private key to call api.请告诉我如何使用此私钥调用 api。

To call API using https you need to configure SSLContext and set it to your HttpClient .要使用https调用 API,您需要配置SSLContext并将其设置为您的HttpClient Refer below sample code.请参阅下面的示例代码。 This is just the sample, you can load keystore and truststore in different way like from classpath, form file system etc.., make the changes accordingly.这只是示例,您可以以不同的方式加载密钥库和信任库,例如从类路径、表单文件系统等,进行相应的更改。

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

    KeyStore identity = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(identity, "password".toCharArray());
    SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
    sslContext.init(
        keyManagerFactory.getKeyManagers(), 
        trustManagerFactory.getTrustManagers(), 
        null
    );
    HttpClient httpClient = HttpClients.custom()
            .setSSLContext(sslContext)
            .setSSLHostnameVerifier(new DefaultHostnameVerifier())
            .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM