【问题标题】:Https / ssl post method work on developer machine but not working on server - javaHttps / ssl post方法在开发人员机器上工作但在服务器上不起作用 - java
【发布时间】:2018-07-18 09:31:02
【问题描述】:

大家好,我使用此代码连接 https 并且它在我的电脑上工作正常,但是当我上传到我的服务器时不工作

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
at mehritco.ir.connection.URLConnection.read(URLConnection.java:38)
at mehritco.ir.cortexclient.objects.invoice.Price.setExchangeRate(Price.java:42)

并且此日志文件不会显示在本地电脑上!

这里是我连接到 https/ssl 的代码

public String readLinkInJson(String url, String data) throws MalformedURLException, IOException {
    URL obj = new URL(url);
    System.out.println(data);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    con.setHostnameVerifier(hv);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setDoOutput(true);
    // For POST only - START
    try (OutputStream os = con.getOutputStream()) {
        os.write(data.getBytes("UTF-8"));
        os.flush();
    }
    // For POST only - END
    String inputLine;
    StringBuilder response = new StringBuilder();
    int responseCode = con.getResponseCode();
    if(responseCode >= 400){
    try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    }
    }else{
    try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getErrorStream()))) {
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    }
    }
    return response.toString();

}

那我该怎么办? 请帮我找到正确的代码...

【问题讨论】:

  • 您是否在远程服务器的 java 信任库中添加了证书?
  • 不,我怎么能? @MançauxPierre-Alexandre
  • 获取您服务器的客户端证书并将其添加到您的 java 信任库中:stackoverflow.com/questions/373295/… 或者如果您没有证书stackoverflow.com/a/23162215/3414468,此评论可以帮助您
  • 使用您的代码,我将设置 requestmethod 更改为 GET 并调用 google.fr 并且它可以工作,请注意 if(responseCode >= 400),在这里您获得的是 Intpustream 而不是 errorStream ...阅读errorStream...如果您正在寻找您的信任库或密钥库文件,它们在jour jvm目录中,谷歌可以帮助您找到它
  • 是的,谢谢@MançauxPierre-Alexandre

标签: java ssl https


【解决方案1】:

我找到了答案!

 TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
        };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (GeneralSecurityException e) {
        }

在制作 url 对象之前添加此代码。

【讨论】:

    猜你喜欢
    • 2011-03-02
    • 2011-05-30
    • 2013-06-17
    • 2017-04-10
    • 2015-08-28
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多