1

我希望在新的 SSL 连接启动和握手开始时收到通知。我需要在调用密钥库之前获取证书。

有没有办法为这个过程注册一个监听器,所以我可以决定证书是否正常,是否应该检查密钥库或立即取消它。

像这样,但对于 SMTP 连接:

URL req = new URL(getUrl);

HttpsURLConnection con = (HttpsURLConnection) req.openConnection();

con.setHostnameVerifier(new HostnameVerifier()
{

    public boolean verify(String hostname, SSLSession session)
    {
        return true; //My decision
    }
});

我正在使用 JAMES 电子邮件服务器 2.3.2(如果这意味着什么)。

先感谢您!

4

1 回答 1

2

您需要设置连接的 SSLFactory。以下示例不使用密钥管理器和默认的 TrustManager。您的检查将进入 checkServerTrusted 方法。

HttpsURLConnection con = (HttpsURLConnection) req.openConnection();
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, // No Key Manager
             new TrustManager[] { new X509TrustManager()
               {
                 @Override
                 public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                     throws CertificateException
                   {}

                 @Override
                 public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                     throws CertificateException {
                      // check the certs
                 }

                 @Override
                 public X509Certificate[] getAcceptedIssuers()
                   {
                     return null;
                   }

               } }, // TrustManager 
             new java.security.SecureRandom());
con.setSSLSocketFactory(context.getSocketFactory());
于 2011-10-19T14:58:10.683 回答