HttpURL连接 SSL 握手被 AWS 签名 URL 重定向中止

HttpURLConnection SSL handshake aborted with AWS signed URL redirect

我正在尝试使用 Android 的 HttpURLConnection 从 Amazon S3 服务器下载文件,但收到 SSLException。我们的服务器是 Nginx 后面的 Node.js 服务器 (Hapi) 运行。该应用程序从我们的服务器请求文件,服务器以 302 重定向回复到 S3 服务器上已签名的 URL 资源。使用重定向回复的代码是:

        var params = {
            Bucket: bucketID, 
            Key: fullPath
        };
        var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
        s3.getSignedUrl('getObject', params, function (err, url)
        {
            if (err || !url)
            {
                return reply(Boom.notFound());
            }

            return reply.redirect(url);

        });

使用浏览器或 curl 工作正常并正确重定向到 S3 并下载文件,但是在 Android 上使用 HttpURLConnection(同样的问题也发生在 HttpClient 上),我得到一个SSLException:

javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x7148d680: I/O error during system call, Connection reset by peer

知道是什么原因造成的吗?

没关系 - 这是应用程序支持的 TLS 版本与服务器支持的 TLS 版本不匹配。服务器是为 TLSv1.1 和 TLSv1.2 设置的,我猜默认情况下 HttpURLConnection 和 HttpClient 是为 TLSv1.0 设置的。我已更改为 HttpClient,因此添加以下代码解决了问题:

        SSLContext sslContext = SSLContexts.createDefault();
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                                                                          new String[]{"TLSv1.1", "TLSv1.2"},
                                                                          null,
                                                                          new NoopHostnameVerifier());
        CloseableHttpClient client = HttpClientBuilder.create()
                .setDefaultCookieStore(cookieStore)
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .build();