且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Java 中以编程方式检查 SSL 证书到期日期

更新时间:2023-11-26 11:02:40

如何从证书中解析过期日期

How to parse the expiration date from the certificate

将其转换为 X509Certificate 并调用 getNotAfter().

Cast it to an X509Certificate and call getNotAfter().

如何确定证书链,例如github证书有链

How to determine the certificate chain, eg, the github certificate with chains

你明白了.这就是 Certificate[] 数组,正如它在 Javadoc.

You've got it. That's what the Certificate[] array is, as it says in the Javadoc.

我怎么知道从哪个证书获取到期日期?

How did i know which certificate to get the expiration date from?

阅读 Javadoc.对等方自己的证书首先是任何证书颁发机构".

Read the Javadoc. "The peer's own certificate first followed by any certificate authorities".

但是我不知道你为什么要这样做.Java 应该已经为您完成了这一切.

However I don't know why you're doing any of this. Java should already do it all for you.

请丢弃不安全且不正确的 TrustManager 实现.处理自签名证书的正确方法是将它们导入客户端信任库.还请扔掉不安全的 HostnameVerifier,并使用默认的或安全的.如果您不希望 HTTPS 安全,为什么还要使用它?

And please throw away that insecure and incorrect TrustManager implementation. The correct way to handle self-signed certificates is to import them into the client truststore. Please also throw away your insecure HostnameVerifier, and use the default one, or a secure one. Why use HTTPS at all if you don't want it to be secure?