我用谷歌搜索了一下有问题。但这些并不能解决我的问题。我在调用休息控件时,代码中的 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;
}
Error is:-I/O error on POST request for "https://myurl.com": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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
有人可以帮我吗?
最佳答案
谢谢大家的帮助。
我已经解决了这个问题
在我的 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;
}
关于java - SPRING BOOT 中的 POST 请求发生 I/O 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569771/