繁体   English   中英

需要在Java中动态导入和使用数字证书

[英]Need to import and use the digital certificate dynamically in java

我正在使用JAX-RPC用Java编写Web服务客户端。此服务提供者正在使用SSL进行身份验证。 而且,我对这种SSL / HTTPS实施完全陌生。 因此,我的要求是导入其数字证书并将其用于发送请求。 但是我不想在本地创建ant trustStore或keystore并从那里导入它。 我想要的是动态地完成上述所有任务,而无需在当前的cacert文件中进行任何更改或创建任何新文件。 希望我的问题很清楚。 提前致谢。

    Finally I got the answer, Just need to import the certificate at run time and inject it while sending the request. It works for me.
    Code snippet is as follows:

    private HttpURLConnection getHTTPConnection(){
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }


                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }


                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                    System.out.println("authType is " + authType);
                    System.out.println("cert issuers");
                    for (int i = 0; i < certs.length; i++) {
                        System.out.println("\t" + certs[i].getIssuerX500Principal().getName());
                        System.out.println("\t" + certs[i].getIssuerDN().getName());
                    }
                }
            } };
            try {
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                URL myurl = new URL("END_POINT");
                connection = (HttpURLConnection)myurl.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
                connection.setFollowRedirects(true);
                connection.setAllowUserInteraction(true);
            } catch (MalformedURLException mue) {
                mue.printStackTrace();
                System.exit(1);
            } catch (IOException ioe) {
                ioe.printStackTrace();
                System.exit(1);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
return connection;
              }

暂无
暂无

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

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