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();
}
}
上一篇: 高州市
下一篇: 磨兽世界*网名逗乐你