java - 线程 "main"javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException 中的异常:找不到受信任的证书

标签 java ssl intellij-idea

我在自动化测试中排名第一。因此,我的任务是为 API 测试创建自动化脚本。我需要使用自签名序列创建 https 请求。该证书为我们的管理员提供了 p12 扩展名和密码文件。我将此证书导出为 .cer 扩展名。然后我创建了信任存储并将此证书导入到该存储。

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:找不到受信任的证书 在 sun.security.ssl.Alerts.getSSLException (Alerts.java:192) 在 sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917) 在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301) 在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295) 在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1471) 在 sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212) 在 sun.security.ssl.Handshaker.processLoop(Handshaker.java:936) 在 sun.security.ssl.Handshaker.process_record(Handshaker.java:871) 在 sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043) 在 sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355) 在 sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 在 java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 在 httpConnection.Connection.testSimpleHttpsClient(Connection.java:179) 在 httpConnection.Connection.main(Connection.java:22) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( native 方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 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:找不到受信任的证书 在 sun.security.validator.SimpleValidator.buildTrustedChain(SimpleValidator.java:384) 在 sun.security.validator.SimpleValidator.engineValidate(SimpleValidator.java:133) 在 sun.security.validator.Validator.validate(Validator.java:260) 在 sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) 在 sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) 在 sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) 在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1453) ... 还有 20 个

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

最佳答案

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

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

关于java - 线程 "main"javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException 中的异常:找不到受信任的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996555/

相关文章:

java - 添加Spring AOP后@Autowired返回null

ssl - 如何将中间证书添加到 pfx 文件

intellij-idea - 如何使“从源滚动”功能始终启用?

java - Xstream 隐式映射作为根元素的属性

java - 当队列已满时增加数组大小的入队方法

java - 训练斯坦福 CRFClassifier 时传递 float 组

ssl - 使用 Camel Jetty 进行基于证书的身份验证

Python mysqlclient 连接失败,在 MariaDB 服务器上配置了 TLSv1.2 或 TLSv1.3

ruby - Intellij IDEA Ruby 语言级别

java - IntelliJ 找不到 google-cloud-storage 类