IBM java POST API引发SSL HandShake异常

Sur*_*alu 5 java api rest websphere

我试图使用IBM Java(1.6)创建post API Rest Call,它是Out of Box of Websphere server 7(Application Server).建立连接时,它会抛出

线程"main"中的异常java.net.SocketException:java.lang.ClassNotFoundException:找不到指定的类com.ibm.websphere.ssl.protocol.SSLSocketFactory

通过这些链接中的这些步骤

https://www.link-intersystems.com/blog/2014/07/20/how-to-fix-java-lang-classnotfoundexception-com-ibm-websphere-ssl-protocol-sslsocketfactory-in-eclipse/

修复该问题后,在发布请求时会抛出一个

线程"main"中的异常javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure

相同的代码适用于Oracle Java(1.6)

下面的代码是我尝试过的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.Security;

public class POST {

     public static void main(String[] args){


        try {
            POSTRequest();
        } catch (IOException e) {
            System.out.println("Error" + e);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
    public static void POSTRequest() throws IOException {
        Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
        Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
        System.out.println("Begin Post Request");
        final String POST_PARAMS = "{\n" + "\"userId\": 151,\r\n" +
            "    \"id\": 101,\r\n" +
            "    \"title\": \"VM Title\",\r\n" +
            "    \"body\": \"Test Body\"" + "\n}";
        System.out.println(POST_PARAMS);
        URL obj = new URL("https://jsonplaceholder.typicode.com/posts");
        HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
        postConnection.setRequestMethod("POST");
        postConnection.setRequestProperty("userId", "a1bcdefgh");
        postConnection.setRequestProperty("Content-Type", "application/json");
        postConnection.setDoOutput(true);
        OutputStream os = postConnection.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        int responseCode = postConnection.getResponseCode();
        System.out.println("POST Response Code :  " + responseCode);
        System.out.println("POST Response Message : " + postConnection.getResponseMessage());
        if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                postConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in .readLine()) != null) {
                response.append(inputLine);
            } in .close();
            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST NOT WORKED");
        }
    }




}
Run Code Online (Sandbox Code Playgroud)

Post_API.txt显示Post_API.txt.

请指教我解决这个问题.

小智 5

您可以尝试在提交给 Web 服务的请求中设置协议。

System.setProperty(“https.protocols”, “TLSv1,TLSv1.1,TLSv1.2”);
Run Code Online (Sandbox Code Playgroud)

此外,需要注意的是修复包的版本和 WebSphere 中使用的 Java 版本。WebSphere 使用与安装捆绑在一起的自己的 Java 版本。

如果 WebSphere 安装的更新版本较低,您可能需要升级 Java 版本。


dbr*_*aux 4

handshake_failure通常与不匹配的 SSL/TLS 协议或密码套件有关。

然而,在这种情况下,如果您查看该站点的 Qualys SSL tester,返回的 4 台服务器显示 Java 6 将因不支持的 SNI 而失败。

因此,我猜测要么 Oracle Java 6 有一些不同的默认 SSL 设置,要么设置了一些内容来覆盖默认行为,或者 WebSphere Java 6 可能是 Java 6 的较旧修复级别,或者是两个 Java 协商 TLS 的顺序级别和密码套件不同。

另请注意,WebSphere 7 现在不再受支持, Java 6也是如此。