线程“main”中的异常javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:找不到可信证书

时间:2014-11-18 14:22:02

标签: java ssl intellij-idea

我处于自动化测试的第一位。因此,我有一项任务是为API测试创建自动脚本。我需要使用自签名的证书创建https请求。这个证书给了我们管理员p12扩展名和密码文件。我将此证书导出为.cer extenssion。比我创建信任库并将此证书导入此商店。

C:\Program Files (x86)\Java\jdk1.8.0_25\jre\bin>keytool -import -keystore clienttrust.jks -file marta.cer -storepass storepass

我在我的项目附近找到了marta.cer和clienttrust.jks。我在Java上用IntelliJ IDEA 13.1.1编写代码。

这是代码:

public void testSimpleHttpsClient() throws CertificateException, InterruptedException, UnrecoverableKeyException, NoSuchAlgorithmException,
        IOException, KeyManagementException, KeyStoreException {


    URL url = new URL("https://intstg1-kaakioskpublicapi.ptstaging.ptec/TLE/36171/player/info");

    HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
    con.setRequestMethod( "GET" );

    SSLContext sslContext = SSLContext.getInstance("TLS");

    char[]  passphrase = "storepass".toCharArray();
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(this.getClass().getResourceAsStream("clienttrust.jks"), passphrase);
    System.out.println(ks);

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);

    System.out.println(tmf.toString());

    HostnameVerifier hostnameVerifier = new HostnameVerifier() {

        public boolean verify(String s, SSLSession sslSession) {
            return s.equals(sslSession.getPeerHost());
        }
    };
    con.setHostnameVerifier(hostnameVerifier);

    sslContext.init(null, tmf.getTrustManagers(), null);
   con.setSSLSocketFactory(sslContext.getSocketFactory());

    int responseCode = con.getResponseCode();
    InputStream inputStream;
    if (responseCode == HttpURLConnection.HTTP_OK) {
        inputStream = con.getInputStream();
    } else {
        inputStream = con.getErrorStream();
    }

    // Process the response
    BufferedReader reader;
    String line = null;
    reader = new BufferedReader( new InputStreamReader( inputStream ) );
    while( ( line = reader.readLine() ) != null )
    {
        System.out.println( line );
    }

    inputStream.close();
}

因此,在执行此代码后,我得到下一个异常:

线程中的异常" main" javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:找不到可信证书     at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)     at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917)     在sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)     在sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295)     at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1471)     在sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)     at sun.security.ssl.Handshaker.processLoop(Handshaker.java:936)     at sun.security.ssl.Handshaker.process_record(Handshaker.java:871)     at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)     at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)     at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)     at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)     在sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)     在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)     在java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)     在httpConnection.Connection.testSimpleHttpsClient(Connection.java:179)     在httpConnection.Connection.main(Connection.java:22)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:483)     在com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 引起:sun.security.validator.ValidatorException:找不到可信证书     at sun.security.validator.SimpleValidator.buildTrustedChain(SimpleValidator.java:384)     at sun.security.validator.SimpleValidator.engineValidate(SimpleValidator.java:133)     at sun.security.validator.Validator.validate(Validator.java:260)     at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)     at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)     at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)     at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1453)     ......还有20个

伙计们,请向我解释如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

将服务器证书导入为受信任的

keytool -import -trustcacerts -alias somealias -keystore clienttrust.jks -file marta.cer