zoukankan      html  css  js  c++  java
  • 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;
                    }
                });
  • 相关阅读:
    Pizza Pie Charts – 基于 Snap SVG 框架的响应式饼图
    超好玩!10款神奇的字符图案 & 词汇云生成工具
    『摄影欣赏』15幅迷人的来自世界各地的婴儿照片【组图】
    CSS 魔法系列:纯 CSS 绘制图形(各种形状的钻石)
    【特别推荐】10款唯美浪漫的婚礼 & 结婚纪念网站模板
    25款创新的 PSD 格式搜索框设计素材【免费下载】
    时尚前沿:15个创意的 3D 字体设计艺术作品欣赏
    Resumable.js – 基于 HTML5 File API 的文件上传
    经典设计:17个最有效的学习着陆页设计的例子
    图标集锦:10套免费的社交媒体 & 社交网站图标
  • 原文地址:https://www.cnblogs.com/zhao1949/p/7409780.html
Copyright © 2011-2022 走看看