I want to check the expiration time of the ca certificate in code. The following is my code, which can display the date, but it is not consistent with the actual due date.

< H2 > I want to check the expiration time of the ca certificate in code. The following is my code, which can display the date, but it is not consistent with the actual due date < / H2 >.

I hope you Daniel can help me to see what the problem is. Thank you

.
import java.io.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class CertificateRemind {

    public static X509Certificate x509Certificate = null;
    public static Process process = null;

    public static void MessageRemind(String url,int port){
        Runtime runtime = Runtime.getRuntime();
        try {
            String connect = "openssl s_client -connect " + url + ":" + port;
            process = runtime.exec(connect);
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            x509Certificate = (X509Certificate) certificateFactory.generateCertificate(process.getInputStream());
            System.out.print((x509Certificate.getNotAfter().getTime()-new Date().getTime())/(24*60*60*1000)+"");

            long date = x509Certificate.getNotAfter().getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            System.out.println(simpleDateFormat.format(date));

        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)  {
        CertificateRemind.MessageRemind("***.***.***",443);
    }
}


the websites of these big companies usually have more than one certificate, and some have many. You may receive different certificates with different connection methods. You can check the certificate signature and frequent issuing institutions, which are different.


have you solved it, buddy, I encounter the same problem, the same annoyance

Menu