javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭连接

时间:2016-12-05 08:52:21

标签: java rest ssl https

我正在使用java 7来运行此代码。我已经将服务器证书添加到我的本地javastore,但它仍然抛出异常。 我从firefox下载了服务器证书并导出,然后使用keytool导入到javaStore。

public class TokenizeClient {

    public static String tokenization(String data){

        byte[] credential = Base64.encodeBase64("username:password".getBytes(StandardCharsets.UTF_8));
        String encodedCrential=new String(credential);
        System.out.println("Encoded Credentials "+credential);


    /*  byte[] encoded = encodedCrential.getBytes();
        // decoding byte array into base64
        byte[] decoded = Base64.decodeBase64(encoded);
        String decodedCredential= new String(decoded, StandardCharsets.UTF_8);
        System.out.println("Decode Credentials "+decodedCredential);*/

        // Tokenize request

        URL tokenize_url;
        String token = null;
        try {

            //tokenize_url = new URL(EncryptionDecryptionConstant.VTS_TOKENIZE_URL);
            tokenize_url = new URL("https://192.168.1.74/vts/rest/v2.0/tokenize");

            System.out.println("tokenizw URL :"+tokenize_url);
            //System.setProperty("https.protocols", "TLSv1");

            HttpsURLConnection httpsConn=(HttpsURLConnection) tokenize_url.openConnection();


            String jStr ="{\"data\":\""+data+"\",\"tokengroup\":\"Enliven-Token-Test\",\"tokentemplate\":\"Enliven-Token-Temp\"}";
            //String jStr ="{\"data\":\"Anees\",\"tokengroup\":\"Enliven-Token-Test\",\"tokentemplate\":\"Enliven-Token-Temp\"}";

            System.out.println("input String :"+jStr);

            httpsConn.setRequestProperty("Content-length", String.valueOf(jStr.length()));
            httpsConn.setRequestProperty("Content-Type","application/json");
            httpsConn.setRequestProperty("Authorization","Basic "+encodedCrential);
            httpsConn.setRequestMethod("POST");
            httpsConn.setDoOutput(true);
            httpsConn.setDoInput(true);

            System.out.println("###### All header properties are set");

            DataOutputStream output = new DataOutputStream(httpsConn.getOutputStream());


            output.writeBytes(jStr);
            output.close();


            BufferedReader rd=new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));

                        String line = "";
            String strResponse = "";
            while ((line = rd.readLine()) != null) {
            strResponse=strResponse+line;
            }
            rd.close();
             System.out.println("********** Response ***"+strResponse);
             token = JsonPath.read(strResponse, "$.token").toString();
            httpsConn.disconnect();

            return token;


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return token;   
    }   

    public static void main(String[] args) {
        String result =VormetricTokenizeClient.tokenization("Anees");
        System.out.println("##############  "+result);
    }
}

例外:

main, WRITE: TLSv1 Handshake, length = 163
main, received EOFException: error
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
main, SEND TLSv1 ALERT:  fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
main, called close()
main, called closeInternal(true)
##############  null
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
    at com.ramyam.security.adapter.VormetricTokenizeClient.tokenization(VormetricTokenizeClient.java:79)
    at com.ramyam.security.adapter.VormetricTokenizeClient.main(VormetricTokenizeClient.java:210)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:482)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    ... 9 more

我可以在调试控制台中看到我添加的证书

0 个答案:

没有答案