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

Java校验.CER、.pem证书格式以及获取证书有效期

程序员文章站 2022-05-19 11:58:01
...
public static void main(String[] args) throws CertificateException, FileNotFoundException {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        FileInputStream in = new  FileInputStream("/Users/name/Downloads/cert.pem");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
        cert.checkValidity();    //校验格式,错误抛出异常,可捕捉
        Date effDate = cert.getNotBefore();
        Date expDate = cert.getNotAfter();
        long diff = expDate.getTime() - effDate.getTime();
        System.out.println(diff / (1000 * 60 * 60 * 24));
        System.out.println(effDate);
        System.out.println(expDate);
    }

 

相关标签: SpringBoot