Jsoup连接到https(密钥库)

时间:2017-08-29 12:54:01

标签: java https jsoup keystore truststore

我遇到连接(登录)到https://jizdenky.regiojet.cz/Login?0的问题。

代码:

//add certificate to trustStore
System.setProperty("javax.net.ssl.trustStore", "keystore/regionjet.jks");
Connection connection = Jsoup.connect("https://jizdenky.regiojet.cz/Login?0");
Connection.Response response = connection.data("passwordAccountCode", username).data("password", password).method(Connection.Method.POST).execute();

我的认证路径仍然是例外

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

任何人都可以帮助我或告诉我哪里有问题?

1 个答案:

答案 0 :(得分:0)

你可以做两件事我只是在这里提供来回答问题。

通常允许所有证书

阅读此答案:https://stackoverflow.com/a/2793153/3977134

相应的代码是:

TrustManager[] trustAllCertificates = new TrustManager[] {
    new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null; // Not relevant.
        }
        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            // Do nothing. Just allow them all.
        }
        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            // Do nothing. Just allow them all.
        }
    }
};

HostnameVerifier trustAllHostnames = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true; // Just allow them all.
    }
};

try {
    System.setProperty("jsse.enableSNIExtension", "false");
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCertificates, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
}
catch (GeneralSecurityException e) {
    throw new ExceptionInInitializerError(e);
}

将证书添加到JRE的商店

此方法要求您从以下网址下载CRT文件。你的浏览器。之后,您应该使用作为JRE一部分的keytool命令将其包含在JRE中。

完整的答案在这里:https://stackoverflow.com/a/7745706/3977134