在Java密钥库中检查证书的到期日期 [英] Checking certificates expiration dates in java keystore

查看:1802
本文介绍了在Java密钥库中检查证书的到期日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java应用程序使用密钥库文件,其中我有一个证书,用于与活动目录服务器的ssl连接。
我要做的是检查其到期日期,并提示用户是否接近到期。我必须这样做,而我的应用程序启动。我的想法是使用外部程序:keytool在密钥库中显示关于某些证书的信息,然后对一个字符串进行一些解析操作,keytool输出找到此验证日期。

My java application uses a keystore file in which I have a certificate which is used in ssl connection with active directory server. What I have to do is to check its expiration date and prompt user if its close to expire. I have to do it while my application starts. My idea is to use external program: keytool to display info about certain certificate in the keystore and then do some parsing operations on a string which keytool outputs to find this validation date.

以下是特定keytool命令的输出:

Here's the output of a specific keytool command:


所有者:
发行者:CN = CPD根CA = cpd,DC =本地

序列号:39e8d1610002000000cb

有效期:Wed Feb 22 21:36:31 CET 2012至:Thu Feb 21 21:36:31 CET 2013
证书指纹:

MD5:82:46:8B:DB:BC:5C:64:21:84:BB:68:E3:4B:D4:35:70 b $ b SHA1:35:52:CA:F2:11:66:1E:50:63:BC:53:A5:50:C1:F0:1E:62:81:BC:3F

签名算法名称:SHA1withRSA

问题是解析日期,因为我不能确定是什么格式

Problem would be with parsing date since I can't be sure in which format it is displayed.

是否有更简单的方法来检查包含在java密钥库文件中的证书的有效期?

Is there any easier way to check expiration date of certificates included in java keystore file?

推荐答案

感谢EJP的方向,这里是一个我想出来的块。

Thanks for the direction EJP, here is a block of what I came up with.

    try {
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(new FileInputStream("keystoreLocation"), "keystorePassword".toCharArray());
        Enumeration<String> aliases = keystore.aliases();
        while(aliases.hasMoreElements()){
            String alias = aliases.nextElement();
            if(keystore.getCertificate(alias).getType().equals("X.509")){
                System.out.println(alias + " expires " + ((X509Certificate) keystore.getCertificate(alias)).getNotAfter());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这篇关于在Java密钥库中检查证书的到期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆