java - 具有 TLS 的 REST 客户端返回 SSLHandshakeException : Received fatal alert: handshake_failure

标签 java rest certificate tls1.2

我正在与 RESTful API 集成,并且我有这个实用方法来发送 REST 请求并获取响应

    BufferedReader br;
        HttpURLConnection con = null;

        try {
            URL obj = new URL(url);
            con = (HttpURLConnection) obj.openConnection();

            con.setReadTimeout(httpReadTimeOut);
            con.setRequestMethod("POST");

            for (String key : requestTypeMap.keySet()) {
                con.setRequestProperty(key, requestTypeMap.get(key));
            }

            String jsonRequest = gson.toJson(request);
            CommonUtilityLogger.debug("json request is :: " + jsonRequest);

            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(jsonRequest);
            wr.flush();
            wr.close();

            if (con.getResponseCode() != 200) {
                CommonUtilityLogger.error("failed : HTTP error code : " + con.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((con.getInputStream())));

            return (T) gson.fromJson(br, responseType);

在第三方 API 文档中,它说我必须使用 TLS 证书,并且它们包含许多与之相关的文件

Test Certificate 1231181189.key 
Test Certificate 1231181189.p7b 
Test Certificate 1231181189.p12 
Test Certificate 1231181189.pem 
TLS Root CA.pem

现在每次我尝试调用这些服务时都会得到

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)

我在这里缺少什么,我找不到它

最佳答案

尝试从浏览器下载 REST API 的证书并将该证书添加到您的 truststore.jks 文件中。

点击此链接创建 keystore 和信任库:Create keystore and truststore

关于java - 具有 TLS 的 REST 客户端返回 SSLHandshakeException : Received fatal alert: handshake_failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538928/

相关文章:

java - 突发 "unable to find valid certification path to requested target"

certificate - x509 证书的字符编码是什么?

web-services - 德尔福HTTPRIO : any way to disable certificate prompt?

Java 向上转型或多态性继承

java - isAnnotationPresent() 不适用于 @Repeatable 注释

java - 如何向cxf客户端调用添加wsa引用参数?

java - Dropwizard Rest API 端点操作

ios - 从 Swift 函数中的异步调用返回数据

java - 我正在尝试用 Java 制作 GUI。我创建了框架,但面板没有显示。怎么了?

ruby-on-rails - Rails 3 嵌套资源路由继承父约束,如何避免?