如何在Java中获取证书信息

时间:2014-03-17 20:43:44

标签: java certificate ssl-certificate

我试图只用Java获取和读取证书。我应该查看哪些代码或示例来获取网站的证书。

例如网站:

  1. https://google.com
  2. https://www.ssllabs.com/
  3. 我是否使用网址类?

    URL url = new URL("https://google.com");
    

1 个答案:

答案 0 :(得分:6)

好的,我刚刚知道如何获取我想要的信息。

public void certInformation(String aURL) throws Exception{
        URL destinationURL = new URL(aURL);
        HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
        conn.connect();
        Certificate[] certs = conn.getServerCertificates();
        for (Certificate cert : certs) {
            System.out.println("Certificate is: " + cert);
            if(cert instanceof X509Certificate) {
                    X509Certificate x = (X509Certificate ) cert;
                    System.out.println(x.getIssuerDN());
            }
        }
    }