java 获取https ssl证书有效期等信息

package com.hknaruto.cmdApp;

import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

public class Main {

    public static void main(String[] args) throws Exception {
        URL url = new URL("https://www.baidu.com");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.connect();
        for (Certificate certificate : connection.getServerCertificates()) {
            //第一个就是服务器本身证书,后续的是证书链上的其他证书
            X509Certificate x509Certificate = (X509Certificate) certificate;
            System.out.println(x509Certificate.getSubjectDN());
            System.out.println(x509Certificate.getNotBefore());//有效期开始时间
            System.out.println(x509Certificate.getNotAfter());//有效期结束时间
        }
        connection.disconnect();
    }
}

 

你可能感兴趣的:(java,信息安全)