javax.net.ssl.SSLHandshakeException : java. security.cert.CertificateException:没有可用的 X509TrustManager 实现

标签 java spring ssl spring-boot

我的应用程序中有代码尝试使用具有指定证书的 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 中删除此依赖项时,一切正常。

关于javax.net.ssl.SSLHandshakeException : java. security.cert.CertificateException:没有可用的 X509TrustManager 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467084/

相关文章:

java - 如何将 .txt/.csv 文件转换为 ORC 格式

java - OpenGL:除以 w 零

java - 连接到 WCF 服务的 Android 应用程序的身份验证(本地环境)

java - 离线环境迁移maven项目

ruby-on-rails - 使用 Apache、Puma、Ubuntu 设置 Rails SSL

java - 监视具有事务方法的服务 spring

java - Spring Data JPA - 使用现有的嵌套子对象保留新对象

java - 我们可以在spring的xml配置中使用hasPermission保护表达式吗?

ruby-on-rails - SSL_connect returned=1 errno=0 state=SSLv3 读取服务器证书 B : certificate verify failed on WINDOWS

android - okhttp 无法正确验证引脚