文章出處

今天研究釘釘的開放平臺,結果一個demo整了半天,這幫助系統寫的也很難懂。遇到兩個問題:

 

1、首先是執行demo時報unable to find valid certification path to requested target,錯誤信息如下:

request url=https://oapi.dingtalk.com/gettoken?corpid=...略...&corpsecret=...略..., exception, msg=sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

只不過經研究,按網上的方式將證書導致到信任庫中也沒用,于是就找了段代碼直接跳過

2、但是報下面這個錯:

request url=https://oapi.dingtalk.com/get_jsapi_ticket?type=jsapi&access_token=2458f4d239173a12809cc94d1915d3b7,
exception, msg=hostname in certificate didn't match: <oapi.dingtalk.com> != <*.laiwang.com> OR <*.laiwang.com>

具體沒空研究,找到下面代碼解決:

    private static CloseableHttpClient getHttpClient() {
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
        ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSF);
        //指定信任密鑰存儲對象和連接套接字工廠
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            //信任任何鏈接
            TrustStrategy anyTrustStrategy = new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            };
            SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
            LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            registryBuilder.register("https", sslSF);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        Registry<ConnectionSocketFactory> registry = registryBuilder.build();
        //設置連接管理器
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
//        connManager.setDefaultConnectionConfig(connConfig);
//        connManager.setDefaultSocketConfig(socketConfig);
        //構建客戶端
        return HttpClientBuilder.create().setConnectionManager(connManager).build();
    }    

 參考:http://blog.csdn.net/shenyunsese/article/details/41075579


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()