从Java建立与HTTPS服务器的连接,并忽略安全证书的有效性 [英] Make a connection to a HTTPS server from Java and ignore the validity of the security certificate

查看:115
本文介绍了从Java建立与HTTPS服务器的连接,并忽略安全证书的有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在测试一个系统,该系统访问一组具有不同密钥的https服务器,其中一些密钥无效,并且所有密钥都不在我的JVM的本地密钥存储区中。我真的只是测试一下,所以我不关心现阶段的安全性。是否有一种很好的方法可以对服务器进行POST调用,并告诉Java不要担心安全证书?

I've been testing a system that accesses a group of https servers with different keys, some of which are invalid and all of them are not in the local key store for my JVM. I am really only testing things out, so I don't care about the security at this stage. Is there a good way to make POST calls to the server and tell Java not to worry about the security certificates?

我的谷歌搜索这些已经提出了一些代码示例做一个类来进行验证,总是有效,但是我不能让它连接到任何服务器。

My google searches for this have brought up some code examples that make a class to do the validation, that always works, but I cannot get it to connect to any of the servers.

推荐答案

根据评论:


使用Google搜索示例,您的意思是这个






更新:链接破了,所以这里是我从互联网档案


Update: the link broke, so here's an extract of relevance which I saved from the internet archive:

// 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(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {

}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {

}

这篇关于从Java建立与HTTPS服务器的连接,并忽略安全证书的有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆