Verifying an SSL certificate expiration with java

We came up with this as a one-off to alert us when a certificate was close to expiration (we were burned one too many times)…

import java.net.*;
import java.security.cert.*;
import javax.net.ssl.*;

public class GetCertDates {
  public static void main(String [] args) throws Exception {
    URL url = new URL(args[0]);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.getResponseCode();
    Certificate[] certificates = conn.getServerCertificates();
    for (Certificate cert:certificates) {
      X509Certificate c = (X509Certificate)cert;
      System.out.println(c.getNotAfter());
    }
    conn.disconnect();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.