簡體   English   中英

javax.net.ssl.SSLHandshakeException:java.security.cert.CertificateException:沒有可用的X509TrustManager實現

[英]javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available

我的應用程序中有代碼,嘗試使用具有指定證書的HTTPS連接到外部主機。

我的代碼如下所示:

    //Load Two Keystores
    KeyStore keystore = KeyStore.getInstance("pkcs12", "SunJSSE");
    InputStream keystoreInput = new FileInputStream(cerPath);
    keystore.load(keystoreInput, passwd.toCharArray());
    System.out.println("Keystore has " + keystore.size() + " keys");

    // load the truststore, leave it null to rely on cacerts distributed with the JVM
    KeyStore truststore = KeyStore.getInstance("pkcs12", "SunJSSE");
    InputStream truststoreInput = new FileInputStream(cerPath);
    truststore.load(truststoreInput, passwd.toCharArray());
    System.out.println("Truststore has " + truststore.size() + " keys");

    //ssl context
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, null, null);
    SSLSocketFactory sf = new SSLSocketFactory(keystore, passwd);
    Scheme https = new Scheme("https", 443, sf);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(https);
    ...
    client = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry));
    ...
    HttpResponse httpResponse = client.execute( targetHost, httpMethod);

變量cerPath包含用於建立連接的證書(xxxx.pfx)的路徑。

在獨立的應用程序中,這可以完美地工作,但是在我的spring-boot應用程序中,相同的代碼將引發以下異常:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available

我已經在普通的Spring應用程序中測試了此代碼,並且它也可以工作。

普通的SSL屬性()也不起作用。

最后,我們發現了問題。

我們在pom.xml中有一個依賴項,指向框架wiremock:

     <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>1.43</version>
        <exclusions>
             <exclusion>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>servlet-api</artifactId>
            </exclusion>
         </exclusions> 
    </dependency>

此框架在jar中包含一個/ keystore文件,該文件會產生此問題。 當我們從pom中刪除此依賴項時,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM