简体   繁体   English

如何获取Java中的证书信息

[英]How to get the Certificate Information in Java

I am trying to get and read a certificate only in Java. 我试图仅在Java中获取和读取证书。 What code or examples should I look at to get the certificates of a website. 我应该查看哪些代码或示例来获得网站的证书。

For examples the websites: 例如,网站:

  1. https://google.com https://google.com
  2. https://www.ssllabs.com/ https://www.ssllabs.com/

Do I use the URL class? 我是否使用URL类?

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

Ok I just found out how I can get the information that I want. 好的,我刚刚发现如何获得所需的信息。

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());
            }
        }
    }

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

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