|
|
@@ -0,0 +1,67 @@ |
|
|
|
package com.hz.pm.api.common.util; |
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUnit; |
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.security.cert.Certificate; |
|
|
|
import java.security.cert.X509Certificate; |
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
/** |
|
|
|
* <p> |
|
|
|
* SSLCertExpiryUtil |
|
|
|
* </p> |
|
|
|
* |
|
|
|
* @author WendyYang |
|
|
|
* @since 14:40 2024/3/8 |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
public class SSLCertExpiryUtil { |
|
|
|
|
|
|
|
private SSLCertExpiryUtil() { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取域名的剩余有效期(单位:天) |
|
|
|
* |
|
|
|
* @param httpUrl \ |
|
|
|
* @return 剩余有效期 |
|
|
|
*/ |
|
|
|
public static Long getLeftValidityPeriod(String httpUrl) { |
|
|
|
Long leftDays = null; |
|
|
|
try { |
|
|
|
URL url = new URL("https://" + httpUrl); |
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
|
|
|
connection.connect(); |
|
|
|
// 获取服务器证书链 |
|
|
|
Certificate[] certificates = connection.getServerCertificates(); |
|
|
|
for (Certificate certificate : certificates) { |
|
|
|
if (certificate instanceof X509Certificate) { |
|
|
|
X509Certificate x509cert = (X509Certificate) certificate; |
|
|
|
// 获取证书的主题 |
|
|
|
String subject = x509cert.getSubjectDN().getName(); |
|
|
|
if (subject.endsWith(httpUrl)) { |
|
|
|
// 获取证书的有效期结束时间 |
|
|
|
Date expiryDate = x509cert.getNotAfter(); |
|
|
|
leftDays = DateUtil.between(new Date(), expiryDate, DateUnit.DAY); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
connection.disconnect(); |
|
|
|
return leftDays; |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("获取域名有效期失败:", e); |
|
|
|
return leftDays; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
System.out.println(getLeftValidityPeriod("zuche.ningdatech.com")); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |