作者/来源:新睿云小编 发布时间:2019-10-29
问题场景:当使用SpringRestTemplate访问自签名证书应用的时候,HTTPS请求失败。
这种情况存在于当时公司部署了一套SAAS应用,我需要使用SpringRestTemplate对该应用进行封装,二次开发一个应用,于是当我使用template.postForEntity的时候,问题来了,因为是自签名证书,所以请求失败了。一个ResourceAccessException异常被抛出 具体信息
1 | org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8088":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 |
于是开始在StackOverflow搜索,发现可以手动禁用Spring RestTemplate的证书校验,最后我自己实现的代码如下
1 | /** |
手动设置RestTemplate后问题解决。