简体   繁体   中英

How to indicate to HttpsUrlConnection the cert (previously imported to cacert file) that it should use

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

I'm trying to consume a web service, in which we import a certificate to the JDK's cacert file. But I can't understand how to "set" the HttpsURLConnection object the cert that it must use to be able to execute the web service.

I read that once the certificate was installed in the cacert file, it was no longer necessary to indicate anything when connecting to the web service.

I am currently skipping the KeyStore and TrustStore validation to invoke the web service.

This is my code:

public void tokenFotoCteNOCHOProd(String url) throws IOException {
    HttpsURLConnection conn = null;
    String params = "";
    String res = "";
    try {
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException {
                    /**/
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException {
                    /**/
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }
        };
            
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        SSLContext sc = SSLContext.getInstance("TLSv1.2");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        params = "someParams";
        URL url = new URL(url);
        conn = (HttpsURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setConnectTimeout(3000);
        try (OutputStream os = conn.getOutputStream()) {
            os.write(params.getBytes(StandardCharsets.UTF_8));
            os.flush();
            res = getRespWS(conn);//get resp..
        }
    } catch (KeyManagementException | NoSuchAlgorithmException | ClassCastException | JsonParseException e) {
        LOGGER.info("error", e);
    }
}
No answers.You can refer to the related questions on the right.
9 Can't import certificate to cacerts

I have a web app that I get redirected to from my CAS server. But I got this exception: So I googled a bit and found a lot of threads about it. I f ...

2016-09-08 12:29:00 1 208   ssl/ cas
暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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