java连接https时禁用证书验证.

import java.io.File;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

    public String test_getssoinfo() throws Exception {

        TrustStrategy acceptingTrustStrategy = new TrustStrategy() {

            @Override
            public boolean isTrusted(java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
                System.out.println("in isTrusted" );
                // TODO 完成方法实现
                return true;
            }
        } ;

        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);

        RestTemplate restTemplate = new RestTemplate(requestFactory);
//        RestTemplate restTemplate = new RestTemplate();
        String userJsonStr = restTemplate.getForObject("https://www.xxx.yy/", String.class);
        return userJsonStr;
    }

okhttp:

X509TrustManager trustManager = new X509TrustManager()
            {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
                {
                }
                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
                {
                }
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers()
                {
                    StrUtil.log("getAcceptedIssuers()");
                    return new X509Certificate[0];
                }
            };

            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new X509TrustManager[] { trustManager }, new java.security.SecureRandom());
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new HostnameVerifier()
            {
                @Override
                public boolean verify(String hostname, SSLSession session)
                {
                    StrUtil.log("verify() hostname:" + hostname + ", session:" + (session != null ? session.toString() : "null"));
                    return true;
                }
            });
时间: 2024-10-12 15:24:59

java连接https时禁用证书验证.的相关文章

Axis 1 https(SSL) client 证书验证错误ValidatorException workaround

Axis 1.x 编写的client在测试https的webservice的时候, 由于client 代码建立SSL连接的时候没有对truststore进行设置,在与https部署的webservice 连接会在运行时报出: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath

python3 urllib.requesturlopen 一个https 时ssl证书错误!

不知道从那个版本起,python用urlopen打开一个https时会验证一次 SSL 证书,当目标使用的是自签名的证书时就会爆出一个 <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)> 解决办法是:通过导入ssl模块,把证书验证关了 import sslssl._create_default_https_context = ssl._create_unverified_

HTTPS请求 SSL证书验证

import urllib2 url = "https://www.12306.cn/mormhweb/" headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} request = urllib2.Request(ur

三步解决fiddler升级后https无法通过证书验证问题

1.      去掉 https 捕获选项 去掉勾选 2.      删除ie下的fiddler证书 将证书下的所有DO_NOT_TRUST_FIDDER证书都删掉 3.      配置https捕获选项 勾选弹出下图 选yes 选yes 再试就OK了 --特殊浏览器处理:firefox 一般装fiddler,如果本地有火狐就会启用fiddlerhook,当然新版火狐升级了安全设置,禁用了此插件,可通过about:config 中 xpinstall.signatures.required 设置

转载-解决使用httpClient 4.3.x登陆 https时的证书报错问题

今天在使用httpClient4.3.6模拟登陆https网站的时候出现了证书报错的问题,这是在开源中国社区里找到的可行的答案(原文链接:http://www.oschina.net/question/194048_135225?sort=time) 大概逻辑是自己实现了信任所有证书 import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apa

调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置-->打开如下图-->在加载用户配置文件选择true 3.如果以上两个方案都不能解决问题,就有可能是加载证书时没有给定证书存储标识 解决方法:在加载证书方法时使用以下方法,请注意第三个参数 X509Certificate2 cer = new X509Certificate2(cerPath, pa

java的https请求解决证书问题

package sqr.srchSpider.utils; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.n

python2/3 https忽略ssl证书验证

import ssl ssl._create_default_https_context = ssl._create_unverified_context InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised

java 连接sqlserver时,表名要注意

Class.forName(driverName); Sql_conn = DriverManager.getConnection(dbURL, userName, userPwd); Statement stmt = Sql_conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from test.dbo.employee"); while(rs.next()){ System.out.println(r