3

我只需要禁用 Java 证书验证以进行测试。所以我理解风险。我使用了以下教程:http ://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html

所以代码是:

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class Cert {
  public static void main(String[] args) throws Exception {
    /*
     *  fix for
     *    Exception in thread "main" 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
     */
    TrustManager[] trustAllCerts = new TrustManager[] {
       new X509TrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
          }

          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }

          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }

       }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    /*
     * end of the fix
     */

    URL url = new URL("https://www.nakov.com:2083/");
    URLConnection con = url.openConnection();
    Reader reader = new InputStreamReader(con.getInputStream());
    while (true) {
      int ch = reader.read();
      if (ch==-1) {
        break;
      }
      System.out.print((char)ch);
    }
      }
    }

我仍然收到以下错误。任何身体都可以帮忙吗?

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: https://www.nakov.com:2083/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at MainProject.Cert.main(Cert.java:56)
4

4 回答 4

4

“修复”和异常似乎无关:修复禁用客户端验证服务器证书,而异常表明服务器认为客户端无权访问该 URL。

于 2012-10-28T02:04:26.530 回答
3

这不会解决问题。401 是通过 HTTPS 和 SSL 传输的,因此证书运行良好。

无论如何,我强烈建议您不要这样做。您不希望在测试和生产中执行不同的代码。测试代码很有可能会泄漏到生产环境中并危及安全性。测试不安全的代码是没有意义的。

于 2012-10-28T02:29:09.770 回答
2

不要费心在您的代码中禁用它,您只需将证书添加到您的测试机器信任库,并 100% 确保您不会在禁用检查的情况下发布构建。

于 2012-10-28T01:57:09.147 回答
1

正如其他人所提到的,错误 401 意味着您确实建立了 SSL 连接,发送了您的请求并被返回了这个 401 错误。所以你的 SSL 代码很好。

当您在浏览器中打开此页面时,您是否收到用户名/密码提示?也许自动登录?如果是这种情况,我会说您的代码缺少此基本身份验证或类似内容。

于 2012-11-20T10:41:18.767 回答