最近在写接口的时候给对方回推数据,发送https请求的时候遇到这么个报错: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 targetHTTPS经由超文本传输协议(HTTP)进行通信,但利用SSL/TLS来加密数据包,这里遇到的问题就是这个安全证书缺少产生的,所以,为了解决这个问题,一般有良好总方法,第一种是将对方网站的证书提前下到本地导入,这里的场景不大适合这个方式,所以采用第二种方式,忽略ssl证书的方法:

具体步骤大致有3步,首先添加一个RestTemplateConfig配置文件,源码如下:

package com.config;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }

    public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
            throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException
    {
        TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());

        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        return factory;
    }
}

第二步,修改初始化RestTemplate类:

RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());

第三步便可以将发送信息写进代码中发送请求了

RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
        MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
        headers.add("Accept", "*/*");
        headers.add("Content-Type", "application/json");
        String requestBody = "{\"returnData\":\"" + str + "\"}";
        System.out.println(str);
        HttpEntity entity = new HttpEntity(requestBody, headers);

        ResponseEntity<String> responseEntity=restTemplate.postForEntity("https://****",entity,String.class);
        //获取返回结果
        String result= responseEntity.getBody();

 工作之余借机记录一下。


版权声明:本文为httpmc2018原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/httpmc2018/article/details/121104952