欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-19 12:02:25
...
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();
    }
}