Java忽略证书验证
我正在尝试创建一些连接到自签名 HTTPS 服务器的示例 Java 项目。我似乎无法让 Java 停止尝试验证证书。我不想必须信任这个证书,我只是想完全忽略所有证书验证;该服务器位于我的网络内部,我希望能够运行一些测试应用程序,而不必担心证书是否有效。
java -Dcom.sun.net.ssl.checkRevocation=false HelloWorld
org.apache.axis2.AxisFault: Connection has been shutdown: 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
-Dcom.sun.net.ssl.checkReplication=false 没有帮助。我还尝试添加以下代码:
public static void DisableCertificateValidation() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
}
但仍然有同样的问题。这是怎么回事?
I'm trying to create some sample Java projects that connect to a self-signed HTTPS server. I can't seem to get Java to stop trying to validate the certificate. I don't want to have to trust this certificate, I just want to ignore all certificate validation altogether; this server is inside my network and I want to be able to run some test apps without worrying about whether the certificate is valid.
java -Dcom.sun.net.ssl.checkRevocation=false HelloWorld
org.apache.axis2.AxisFault: Connection has been shutdown: 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
The -Dcom.sun.net.ssl.checkRevocation=false didn't help. I also tried adding the following code:
public static void DisableCertificateValidation() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
}
But still have the same issue. What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
org.apache.axis2.AxisFault
表示您正在使用 Axis 2,并且 Axis 2 不使用HttpsURLConnection
建立 HTTP(S) 连接,而是使用 Apache HttpClient (据我所知是 3.x),所以HttpsURLConnection.setDefaultSSLSocketFactory(...)
在那里没有任何效果。您可以查看关于设置Axis 2 的
SSLContext
,更具体地说,本文档:http://axis.apache.org/axis2/java/ core/docs/http-transport.html#httpsupport(或者,您可以使用
SSLContext.setDefault(...)< 设置默认的
SSLContext
/code>,Java中引入6. 在实际应用程序中,禁用默认 SSL 上下文的证书验证显然不是一个好主意。)org.apache.axis2.AxisFault
indicates that you're using Axis 2, and Axis 2 doesn't useHttpsURLConnection
to make its HTTP(S) connections, but Apache HttpClient (3.x as far as I know), soHttpsURLConnection.setDefaultSSLSocketFactory(...)
will have no effect there.You can have a look at this answer about setting up an
SSLContext
for Axis 2, more specifically, this document: http://axis.apache.org/axis2/java/core/docs/http-transport.html#httpsupport(Alternatively, you may be able to get away with setting the default
SSLContext
withSSLContext.setDefault(...)
, introduced in Java 6. Disabling certificate verification for your default SSL context is obviously not a good idea in a real application.)这是一个较老的问题,但我偶然发现了它,它有点推动我走向正确的方向。通过设置参数,我可以在没有有效客户端证书的情况下访问 axis2 中的 https url:
只需确保在调用 axis2 服务客户端之前执行此操作。这不是我在生产中会做的事情,但作为对不安全服务器的快速破解,它为我做到了这一点。
This is an older question but I stumbled upon it and it kinda psuhed me in the right diretion. I could access an https url in axis2 without a valid client certificate by setting the params:
Just be sure to do this before calling the axis2 service client. This is nothing I would do in production, but as a fast hack for an unsecured server that did the trick for me.