Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中请求URL时忽略证书错误_Java_Ssl Certificate - Fatal编程技术网

在Java中请求URL时忽略证书错误

在Java中请求URL时忽略证书错误,java,ssl-certificate,Java,Ssl Certificate,我试图打印一个URL(根本不涉及浏览器),但该URL当前抛出以下内容: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to re

我试图打印一个URL(根本不涉及浏览器),但该URL当前抛出以下内容:

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
我使用JEditorPane的setPage方法调用URL,该方法仅将URL作为参数。假设我不能在服务器端更改任何内容,并且仍然需要访问此资源,那么我如何忽略证书错误(或其他使我达到目标的内容)


通过浏览器访问此URL会告诉我该网站不受信任,并询问我是否要继续

这可能是因为您的密钥库中用于访问目标
HTTPS
URL的证书与来自服务器的证书不匹配

您需要将证书导入到JVM的密钥库中。 获取密钥库证书以访问此URL,然后使用将其导入主密钥库

keytool-importkeystore-srckeystore/path/to/custom/keystore-destkeystore$JAVA_HOME/jre/lib/security/cacerts


假设您正在使用来自
$Java\u HOME
的Java来覆盖该方法

在该方法中,您可以测试它是否是一个。如果是,您可以使用一个不为连接执行任何and的自定义。然后返回
InputStream


这将挫败运行时保护用户免受恶意软件欺骗站点攻击的任何尝试。如果这真的是您想要的…

我会使用埃里克森解决方案。
另一种方法是将服务器的证书(可能是自签名的)添加到您的可信证书密钥库中(我只会在您的测试环境中这样做)。
使用:

创建jssecacerts文件的步骤 然后将其复制到以下文件夹:

$JAVA_HOME/jre/lib/security 

如果您每次都要联系的是同一台服务器,那么只需将该证书添加到信任存储中,就可以信任该证书。我不会将其添加到默认的cacerts文件中,但是您可以创建自己的密钥库,并通过javax.net.ssl.trustStore系统属性指定它


最后,如果您想完全禁用PKIX检查(只有当您知道这会严重危害安全性时,才应该这样做)唯一的方法是按照erickson的建议实现您自己的SSL上下文和信任管理器。

相关说明:我与WCF.net Web服务的相互认证在我的测试环境中造成了问题。我将这两行代码添加到我的代码中以解决问题,并允许我解决该问题:

//Uncomment this in case server demands some unsafe operations       
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");       

//this command allowed me to see the detailed debugger log for detecting the
//issue with my certs.
System.setProperty("javax.net.debug","all");

下面是一个很好的参考资料,用于在Java/Windows/Unix世界中处理SSL协商和认证:

事实可能的确如此。也看到
//Uncomment this in case server demands some unsafe operations       
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");       

//this command allowed me to see the detailed debugger log for detecting the
//issue with my certs.
System.setProperty("javax.net.debug","all");