我遇到连接(登录)到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
任何人都可以帮助我或告诉我哪里有问题?
答案 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);
}
此方法要求您从以下网址下载CRT
文件。你的浏览器。之后,您应该使用作为JRE一部分的keytool
命令将其包含在JRE中。