{"id":4586,"date":"2019-12-10T20:42:03","date_gmt":"2019-12-10T12:42:03","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4586"},"modified":"2019-12-10T20:42:03","modified_gmt":"2019-12-10T12:42:03","slug":"%e4%bf%ae%e5%a4%8d-caused-by-sun-security-validator-validatorexception-pkix-path-building-failed-%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4586","title":{"rendered":"\u4fee\u590d Caused by: sun.security.validator.ValidatorException: PKIX path building failed \u5f02\u5e38"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u95ee\u9898\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u5176\u5b9e\u5f88\u957f\uff0c\u6807\u9898\u4e2d\u6211\u53ea\u622a\u53d6\u4e86\u4e00\u90e8\u5206\u3002\u5982\u679c\u5168\u90e8\u5217\u53d6\u51fa\u6765\uff0c\u6211\u4f30\u8ba1\u4f1a\u5f71\u54cd\u641c\u7d22\u5f15\u64ce\u7684\u6536\u5f55\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u8fd9\u4e2a\u624d\u662f\u5b8c\u6574\u7684\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fa\u73b0\u8fd9\u4e2a\u9519\u8bef\u7684\u539f\u56e0\u662f https \u52a0\u5bc6\u8fc7\u7a0b\u4e2d\uff0c\u8bc1\u4e66\u9a8c\u8bc1\u6ca1\u901a\u8fc7\u3002\u6309\u7167\u6211\u4e0b\u9762\u7684\u4ee3\u7801\u8c03\u6574\u4e00\u4e0b\uff0c\u5c31\u53ef\u4ee5\u89e3\u51b3\u6389\u8fd9\u4e2a\u9519\u8bef\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class HttpClientUtils {\n    public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, \n            NoSuchAlgorithmException, KeyManagementException {\n        HttpClientBuilder b = HttpClientBuilder.create();\n        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){\n            public boolean isTrusted(X509Certificate[] x509Certificates, String s) {\n                return false;\n            }\n        }).build();\n        b.setSSLContext(sslContext);\n        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;\n        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);\n        Registry&lt;ConnectionSocketFactory&gt; socketFactoryRegistry = RegistryBuilder.&lt;ConnectionSocketFactory&gt;create()\n                .register(\"http\", PlainConnectionSocketFactory.getSocketFactory())\n                .register(\"https\", sslSocketFactory)\n                .build();\n        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);\n        connMgr.setMaxTotal(200);\n        connMgr.setDefaultMaxPerRoute(100);\n        b.setConnectionManager( connMgr);\n        CloseableHttpClient client = b.build();\n        return client;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">RestTemplate \u518d\u8c03\u6574\u4e00\u4e0b\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration\npublic class RestTemplateConfig {\n    @Bean\n    public RestTemplate httpsRestTemplate(HttpComponentsClientHttpRequestFactory httpsFactory) {\n        RestTemplate restTemplate = new RestTemplate(httpsFactory);\n        restTemplate.setErrorHandler(\n                new ResponseErrorHandler() {\n                    public boolean hasError(ClientHttpResponse clientHttpResponse) {\n                        return false;\n                    }\n                    public void handleError(ClientHttpResponse clientHttpResponse) {\n                        \/\/ \u9ed8\u8ba4\u5904\u7406\u975e200\u7684\u8fd4\u56de\uff0c\u4f1a\u629b\u5f02\u5e38\n                    }\n                });\n        return restTemplate;\n    }\n    @Bean(name = \"httpsFactory\")\n    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory()\n            throws Exception {\n        CloseableHttpClient httpClient = HttpClientUtils.acceptsUntrustedCertsHttpClient();\n        HttpComponentsClientHttpRequestFactory httpsFactory =\n                new HttpComponentsClientHttpRequestFactory(httpClient);\n        httpsFactory.setReadTimeout(40000);\n        httpsFactory.setConnectTimeout(40000);\n        return httpsFactory;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class RestTemplateTest {\n    @Autowired\n    private RestTemplate restTemplate;\n    @Test\n    public void testHttps(){\n        String url = \"https:\/\/free-api.heweather.com\/v5\/forecast?city=CN101080101&amp;key=5c043b56de9f4371b0c7f8bee8f5b75e\";\n        String resp = restTemplate.getForObject(url, String.class);\n        System.out.println(resp);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e Maven \u4e2d\u9700\u8981\u5f15\u5165\u4e00\u4e0b\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.httpcomponents&lt;\/groupId&gt;\n    &lt;artifactId&gt;httpclient&lt;\/artifactId&gt;\n    &lt;version&gt;4.5.3&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u5c31\u53ef\u4ee5\u505a\u5230\uff0cRestTemplate \u53d1\u9001 HTTP\u3001HTTPS \u8bf7\u6c42\u4e86\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4586\">\u4fee\u590d Caused by: sun.security.validator.ValidatorException: PKIX path building failed \u5f02\u5e38<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u5176\u5b9e\u5f88\u957f\uff0c\u6807\u9898\u4e2d\u6211\u53ea\u622a\u53d6\u4e86\u4e00\u90e8\u5206\u3002\u5982\u679c\u5168\u90e8\u5217\u53d6\u51fa\u6765\uff0c\u6211\u4f30\u8ba1\u4f1a\u5f71\u54cd\u641c\u7d22\u5f15\u64ce\u7684\u6536\u5f55\u3002 \u4e0b\u9762\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[995,1906,590,994,1530],"class_list":["post-4586","post","type-post","status-publish","format-standard","hentry","category-java","tag-https","tag-resttemplate","tag-springboottest","tag-ssl"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4586"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4586\/revisions"}],"predecessor-version":[{"id":4587,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4586\/revisions\/4587"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}