HttpsDto httpsDto= SSLHttpsUtil.gethttpsSsl(url);
先调用对应方法,url为地址
public static HttpsDto gethttpsSsl(String urlString){
HttpsDto result = new HttpsDto();
Boolean bo = false;
HttpsURLConnection connection=null;
try {
connection = (HttpsURLConnection) new java.net.URL(urlString).openConnection();
connection.connect();
X509Certificate cert = (X509Certificate) connection.getServerCertificates()[0];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String kstime= sdf.format(cert.getNotBefore());
System.out.println("证书有效期开始: " + kstime);
String yxtime= sdf.format(cert.getNotAfter());
System.out.println("证书有效期至: " + yxtime);
result.setKstime(kstime);
result.setYxtime(yxtime);
// 检查证书是否已过期
if (cert.getNotAfter().before(new Date())) {
System.out.println("证书已过期。");
} else if (cert.getNotBefore().after(new Date())) {
System.out.println("证书尚未生效。");
} else {
bo=true;
System.out.println("证书有效。");
}
result.setZhstatus(bo);
} catch (Exception e) {
log.info("检查证书报错");
} finally {
connection.disconnect();
}
return result;
}