首先,我知道之前有人问过,但是我尝试了所提出的解决方案并没有得到任何结果。
我正在尝试在java中开发一个简单的程序,它连接到一个网站并从那里托管的文本文件中读取。起初我认为证书会导致问题,因为我无法在没有收到警告的情况下在firefox上打开网站。 Internet Explorer不会产生任何问题。代码如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;
public class insig
{
public static void main(String[] args) throws IOException
{
URL fpath = new URL("website/test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fpath.openStream()));
String Reader = null;
while((Reader = br.readLine()) != null)
{
System.out.println(Reader);
}
}
}
我尝试的第一件事是在Java: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target上提出的解决方案,没有成功。试图调试,虽然我不太了解这一点,但在http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/ReadDebug.html的帮助下,我能够理解,直到寻找可信证书的那一部分才开始。已经检查我是否将证书添加到jre的正确版本并仍然有相同的问题。
网站所有者可以解决问题吗?这只是因为这不会只在我的电脑上运行,所以在每台运行的PC上再次配置一切都不方便。
答案 0 :(得分:0)
同样,有三种方式:
在JRE存储中添加证书链(是的,使用keytool命令)。
创建自己的商店(再次使用keytool)将其与您的程序一起发送并将其设置为-D属性。
创建您自己的验证。
在链接中,我看到了我的评论:
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
//Vadim: here is the place to check client certs and throw an exception if certs are wrong. When there is nothing all certs accepted.
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
//Vadim: here is the place to check server certs and throw an exception if certs are wrong. When there is nothing all certs accepted.
}
} };
// Install the all-trusting trust manager
final 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) {
// Here is the palce to check host name against to certificate owner
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
URL url = new URL("https://www.google.com");
如果您将按原样使用它,则不会有任何证书验证 - 所有证书和所有主机都被接受。