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

前端 未结 8 726
旧巷少年郎
旧巷少年郎 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: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);
        }
    }
    

提交回复
热议问题