How to ignore PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException?

前端 未结 8 667
旧巷少年郎
旧巷少年郎 2020-11-28 03:29

I got the following exception when try to post a request to a http server:

Here is the code I used

URL url = new URL(
        \"https://www.abc.com\"         


        
相关标签:
8条回答
  • 2020-11-28 04:06

    Set validateTLSCertificates property to false for your JSoup command.

    Jsoup.connect("https://google.com/").validateTLSCertificates(false).get();
    
    0 讨论(0)
  • 2020-11-28 04:08

    If you want to ignore the certificate all together then take a look at the answer here: Ignore self-signed ssl cert using Jersey Client

    Although this will make your app vulnerable to man-in-the-middle attacks.

    Or, try adding the cert to your java store as a trusted cert. This site may be helpful. http://blog.icodejava.com/tag/get-public-key-of-ssl-certificate-in-java/

    Here's another thread showing how to add a cert to your store. Java SSL connect, add server cert to keystore programmatically

    The key is:

    KeyStore.Entry newEntry = new KeyStore.TrustedCertificateEntry(someCert);
    ks.setEntry("someAlias", newEntry, null);
    
    0 讨论(0)
  • 2020-11-28 04:13

    If you're using CloudFoundry then you'd have to explicitly push the jar along with the keystore having the certificate.

    0 讨论(0)
  • 2020-11-28 04:16

    I also faced this issue. I was having JDK 1.8.0_121. I upgraded JDK to 1.8.0_181 and it worked like a charm.

    0 讨论(0)
  • 2020-11-28 04:23

    FWIW, on Ubuntu 10.04.2 LTS installing the ca-certificates-java and the ca-certificates packages fixed this problem for me.

    0 讨论(0)
  • 2020-11-28 04:26

    I got the same error while executing the below spring-boot + RestAssured simple test.

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import static com.jayway.restassured.RestAssured.when;
    import static org.apache.http.HttpStatus.SC_OK;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    public class GeneratorTest {
    
    @Test
    public void generatorEndPoint() {
        when().get("https://bal-bla-bla-bla.com/generators")
                .then().statusCode(SC_OK);
        }
    }
    

    The simple fix in my case is to add 'useRelaxedHTTPSValidations()'

    RestAssured.useRelaxedHTTPSValidation();
    

    Then the test looks like

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import static com.jayway.restassured.RestAssured.when;
    import static org.apache.http.HttpStatus.SC_OK;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    public class GeneratorTest {
    
    @Before
    public void setUp() {
       RestAssured.useRelaxedHTTPSValidation();
    }
    
    
    @Test
    public void generatorEndPoint() {
        when().get("https://bal-bla-bla-bla.com/generators")
                .then().statusCode(SC_OK);
        }
    }
    
    0 讨论(0)
提交回复
热议问题