首页 > 解决方案 > 对 url 的 POST 请求的 I/O 错误:收到致命警报:handshake_failure Springboot resttemplate

问题描述

我正在尝试通过 SpringRestTemplate在 Sprin-Boot 应用程序中连接安全 REST API。我收到以下错误:

I/O error on POST request for \"URL\": Received fatal alert: handshake_failure; nested exception is javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

RestTemplate豆类配置:

public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, FileNotFoundException, IOException {
        
        char[] all = "mypassword".toCharArray();
        KeyStore ks = KeyStore.getInstance("JKS");
        try (InputStream stream = this.getClass().getClassLoader().getResourceAsStream("mycert.jks")) {
            ks.load(stream, all);
            IOUtils.closeQuietly(stream);
        }

        SSLContext sslContext = new SSLContextBuilder()
                .loadKeyMaterial(ks, all).loadTrustMaterial(new TrustSelfSignedStrategy()).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
        
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        clientBuilder.setSSLSocketFactory(csf);
        
        CloseableHttpClient httpClient = clientBuilder.build();

        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        requestFactory.setBufferRequestBody(false);

        return new RestTemplate(requestFactory);
        
}

应用程序(Pod)控制台日志:

--------> o.s.web.client.RestTemplate              : HTTP POST https://URL--------> o.s.web.client.RestTemplate              : Accept=[text/plain, application/json, application/*+json, */*]--------> o.s.web.client.RestTemplate              : Writing [com.ril.model.json.request] as "application/json"--------> o.a.h.client.protocol.RequestAddCookies  : CookieSpec selected: default--------> o.a.h.client.protocol.RequestAuthCache   : Auth cache not set in the context--------> h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {s}->https://url:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]--------> h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 0][route: {s}->https://URL:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]--------> o.a.http.impl.execchain.MainClientExec   : Opening connection {s}->https://url:443--------> .i.c.DefaultHttpClientConnectionOperator : Connecting to url/10.21.252.51:443--------> o.a.h.c.ssl.SSLConnectionSocketFactory   : Connecting socket to url/10.21.252.51:443 with timeout 0--------> o.a.h.c.ssl.SSLConnectionSocketFactory   : Enabled protocols: [TLSv1, TLSv1.1, TLSv1.2]--------> o.a.h.c.ssl.SSLConnectionSocketFactory   : Enabled cipher suites:[TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]--------> o.a.h.c.ssl.SSLConnectionSocketFactory   : Starting handshake--------> h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: Shutdown connection--------> o.a.http.impl.execchain.MainClientExec   : Connection discarded--------> h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: Close connection--------> h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 0][route: {s}->https://URL:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]--------> BLActualizationUpdateResponseServiceImpl : Failed to get remote resource because: I/O error on POST request for "url": Received fatal alert: handshake_failure; nested exception is javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure-------->com.ril.aop.LogAop                       : Exception in ===: -------->com.ril.aop.LogAop                       : org.springframework.web.client.ResourceAccessException: I/O error on POST request for "url": Received fatal alert: handshake_failure; nested exception is javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

我尝试了很多方法来解决错误。

这是来自目标 API 的 chrome 开发人员工具安全选项卡输出

标签: spring-bootsslresttemplatesslhandshakeexception

解决方案


推荐阅读