証明書を動的にインポートする場合は、カスタムx509TrustManager
. これは、またはSSLContext
の作成に使用されるの設定時に行われます。SSLSocketFactory
SSLEngine
jSSLutilsは、既存のトラスト マネージャーをラップし、特定の設定をカスタマイズできるライブラリです。必要ありませんが、役立つ場合があります。
これは次のようになります。
PKIXSSLContextFactory sslContextFactory = new PKIXSSLContextFactory();
sslContextFactory.setTrustManagerWrapper(new X509TrustManagerWrapper() {
@Override
public X509TrustManager wrapTrustManager(final X509TrustManager origManager) {
return new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return origManager.getAcceptedIssuers();
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
try {
// This will call the default trust manager
// which will throw an exception if it doesn't know the certificate
origManager.checkServerTrusted(chain, authType);
} catch (CertificateException e) {
// If it throws an exception, check what this exception is
// the server certificate is in chain[0], you could
// implement a callback to the user to accept/refuse
}
}
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
origManager.checkClientTrusted(chain, authType);
}
};
}
});
SSLContext sslContext = sslContextFactory.buildSSLContext();
((PKIX)SSLContextFactory
とX509TrustManagerWrapper
は jSSLutils から来ていますが、残りは J2SE/J2EE で利用できます。)
キャッチしたい がいくつかあります (サブクラスを参照してくださいCertificateException
)。ユーザーにコールバックを行う場合、SSL/TLS ハンドシェイクのタイムアウトが原因で、SSL/TLS 接続が最初に失敗する可能性があります (コールバックの応答に時間がかかりすぎる場合)。
SSLContext
これを (Java 6 から)デフォルトとして使用できますが、それSSLContext.setSSLContext(...)
は必ずしも良い考えではありません。可能であればSSLContext
、SSL/TLS 接続を確立するライブラリに を渡します。これがどのように行われるかはさまざまですが、たとえば Apache HTTP Client 4.x には SSL 設定を構成するための複数のオプションがあり、そのうちの 1 つはKeyStore
s を渡し、もう 1 つはSSLContext
.
また、接続しようとしているオブジェクトごとではなく、スレッドごとに何かを行うこともできます (ライブラリに依存) X509TrustManager
。トラストマネージャー。