Java: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

前端 未结 23 1572
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:44

I have a class that will download a file from a https server. When I run it, it returns a lot of errors. It seems that I have a problem with my certificate

23条回答
  •  眼角桃花
    2020-11-22 05:01

    This solved my issue,

    We need to import the cert onto the local java. If not we could get the below exception.

        javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
            at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
            at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
            at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    

    SSLPOKE is a tool where you can test the https connectivity from your local machine.

    Command to test the connectivity:

    "%JAVA_HOME%/bin/java" SSLPoke  443
    
        sun.security.validator.ValidatorException: PKIX path building failed: 
        sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
            at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
            at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
            at sun.security.validator.Validator.validate(Validator.java:260)
            at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
            at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
            at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
            at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1496)
            at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
            at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
            at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
            at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
            at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
            at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
            at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
            at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)
            at SSLPoke.main(SSLPoke.java:31)
        Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to 
        requested target
            at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
            at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
            at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
            at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
            ... 15 more
    
    keytool -import -alias  -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file 
    

    this would first prompt to "Enter keystore password:" changeit is the default password. and finally a prompt "Trust this certificate? [no]:", provide "yes" to add the cert to keystore.

    Verfication:

    C:\tools>"%JAVA_HOME%/bin/java" SSLPoke  443
    Successfully connected    
    

提交回复
热议问题