java - SPRING BOOT 中的 POST 请求发生 I/O 错误

标签 java spring-boot ssl

我用谷歌搜索了一下有问题。但这些并不能解决我的问题。我在调用休息控件时,代码中的 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/

相关文章:

java - 将数据放入 map 时 Hazelcast、HazelcastSerializationException

java - MyBatis:将字符串映射到 boolean 值

java - 为什么当参数以(.pl)结尾时,Spring MVC @RequestMapping 抛出 406 映射错误(/user/{username :. +})

java - 即使在安装和验证证书后仍出现 SSL 握手错误

java - 如何在java中的嵌套循环中中断if语句

java - 在 Spring Cloud Sleuth 和 MDC 中使用 Baggage 时如何避免手动清理?

java - 如何使用 Spring mvc 和 MyBatis 获取数据连接四个表

java - 在 Tomcat Apache Web 服务器上安装通配符 SSL 证书(由 Comodo 提供)

unit-testing - 用于单元测试的模拟 SSL HttpRequest

java - 为什么模糊图像得到更好的拉普拉斯分数方差?