SPRINGBOOT中POST请求的I / O错误

时间:2019-04-08 09:04:48

标签: java spring-boot ssl

我已经用问题用谷歌搜索了。但这无法解决我的问题。调用rest控件时,我的代码中的SSL证书有问题。

我的控制方法是:

@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/token", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody TokenModel GetToken(@RequestBody RequestBodyJson requestBodyJson) {
    TokenModel response = null; 
    RestTemplate restTemplate = new RestTemplate();
    try {                          
        response = new GenericRestClient<RequestBodyJson, TokenModel>().execute(
            new RequestDetails(url, HttpMethod.POST), requestBodyJson, responseHandler,TokenModel.class);
        restTemplate.getForEntity(url , TokenModel.class);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return response;
}
  

错误为:-“ https://myurl.com”的POST请求发生I / O错误:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的到达要求目标的认证路径;嵌套的异常是javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到到请求目标的有效认证路径

有人可以在这里帮助我吗?

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助。

我已通过

解决了问题

在我的POM.XML中,我添加了一个依赖项

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5</version>
</dependency>

然后我的JAVA代码是

@RequestMapping(value = "/gettokens", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody ResponseEntity<TokenModel> GetTokens(@RequestBody RequestBodyJson requestBodyJson)
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    ResponseEntity<TokenModel> response = null;
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
            .loadTrustMaterial(null, acceptingTrustStrategy).build();
    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
    CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(csf).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);
    RestTemplate restTemplate = new RestTemplate(requestFactory);       
    try {                          
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        Map map = new HashMap<String, String>();
        map.put("Content-Type", "application/json");
        headers.setAll(map);
        HttpEntity<?> _HttpEntityRequestBodyJson = new HttpEntity<>(requestBodyJson, headers); 
        response= restTemplate.exchange(url, HttpMethod.POST,_HttpEntityRequestBodyJson, new ParameterizedTypeReference<TokenModel>() {});  
        System.out.println(response.getBody());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return response;
}