幾行代碼輕松搞定Retrofit 2.0 https的支持

網(wǎng)上或許零零散散有解決Retrofit https的問題待锈,現(xiàn)在做個(gè)總結(jié)吧
首先初始化證書蝙茶,你需要一個(gè)httpsUtils,代碼如下

    import java.io.IOException;
    import java.io.InputStream;
    import java.security.KeyManagementException;
    import java.security.KeyStore;
    import java.security.KeyStoreException;
    import java.security.NoSuchAlgorithmException;
    import java.security.UnrecoverableKeyException;
    import java.security.cert.CertificateException;
    import java.security.cert.CertificateFactory;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.KeyManager;
    import javax.net.ssl.KeyManagerFactory;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.TrustManagerFactory;
    import javax.net.ssl.X509TrustManager;
    
    /**
     * describe https 綁定證書工具類
     *
     * @author emilia.Zhang
     * @time 2017/8/11 9:43.
     */
    public class HttpUtils {
        public static class SSLParams
        {
            public SSLSocketFactory sSLSocketFactory;
            public X509TrustManager trustManager;
        }
    
        public static SSLParams getSslSocketFactory(InputStream[] certificates, InputStream bksFile, String password)
        {
            SSLParams sslParams = new SSLParams();
            try
            {
                TrustManager[] trustManagers = prepareTrustManager(certificates);
                KeyManager[] keyManagers = prepareKeyManager(bksFile, password);
                SSLContext sslContext = SSLContext.getInstance("TLS");
                X509TrustManager trustManager = null;
                if (trustManagers != null)
                {
                    trustManager = new MyTrustManager(chooseTrustManager(trustManagers));
                } else
                {
                    trustManager = new UnSafeTrustManager();
                }
                sslContext.init(keyManagers, new TrustManager[]{trustManager},null);
                sslParams.sSLSocketFactory = sslContext.getSocketFactory();
                sslParams.trustManager = trustManager;
                return sslParams;
            } catch (NoSuchAlgorithmException e)
            {
                throw new AssertionError(e);
            } catch (KeyManagementException e)
            {
                throw new AssertionError(e);
            } catch (KeyStoreException e)
            {
                throw new AssertionError(e);
            }
        }
    
        private class UnSafeHostnameVerifier implements HostnameVerifier
        {
            @Override
            public boolean verify(String hostname, SSLSession session)
            {
                return true;
            }
        }
    
        private static class UnSafeTrustManager implements X509TrustManager
        {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException
            {
            }
    
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException
            {
            }
    
            @Override
            public X509Certificate[] getAcceptedIssuers()
            {
                return new java.security.cert.X509Certificate[]{};
            }
        }
    
        private static TrustManager[] prepareTrustManager(InputStream... certificates)
        {
            if (certificates == null || certificates.length <= 0) return null;
            try
            {
    
                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                keyStore.load(null);
                int index = 0;
                for (InputStream certificate : certificates)
                {
                    String certificateAlias = Integer.toString(index++);
                    keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
                    try
                    {
                        if (certificate != null)
                            certificate.close();
                    } catch (IOException e)
    
                    {
                    }
                }
                TrustManagerFactory trustManagerFactory = null;
    
                trustManagerFactory = TrustManagerFactory.
                        getInstance(TrustManagerFactory.getDefaultAlgorithm());
                trustManagerFactory.init(keyStore);
    
                TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    
                return trustManagers;
            } catch (NoSuchAlgorithmException e)
            {
                e.printStackTrace();
            } catch (CertificateException e)
            {
                e.printStackTrace();
            } catch (KeyStoreException e)
            {
                e.printStackTrace();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
    
        }
    
        private static KeyManager[] prepareKeyManager(InputStream bksFile, String password)
        {
            try
            {
                if (bksFile == null || password == null) return null;
    
                KeyStore clientKeyStore = KeyStore.getInstance("BKS");
                clientKeyStore.load(bksFile, password.toCharArray());
                KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                keyManagerFactory.init(clientKeyStore, password.toCharArray());
                return keyManagerFactory.getKeyManagers();
    
            } catch (KeyStoreException e)
            {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e)
            {
                e.printStackTrace();
            } catch (UnrecoverableKeyException e)
            {
                e.printStackTrace();
            } catch (CertificateException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
    
        private static X509TrustManager chooseTrustManager(TrustManager[] trustManagers)
        {
            for (TrustManager trustManager : trustManagers)
            {
                if (trustManager instanceof X509TrustManager)
                {
                    return (X509TrustManager) trustManager;
                }
            }
            return null;
        }
    
    
        private static class MyTrustManager implements X509TrustManager
        {
            private X509TrustManager defaultTrustManager;
            private X509TrustManager localTrustManager;
    
            public MyTrustManager(X509TrustManager localTrustManager) throws NoSuchAlgorithmException, KeyStoreException
            {
                TrustManagerFactory var4 = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                var4.init((KeyStore) null);
                defaultTrustManager = chooseTrustManager(var4.getTrustManagers());
                this.localTrustManager = localTrustManager;
            }
    
    
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
            {
    
            }
    
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
            {
                try
                {
                    defaultTrustManager.checkServerTrusted(chain, authType);
                } catch (CertificateException ce)
                {
                    localTrustManager.checkServerTrusted(chain, authType);
                }
            }
    
    
            @Override
            public X509Certificate[] getAcceptedIssuers()
            {
                return new X509Certificate[0];
            }
        }
    
    }

然后在你application初始化

  public class AppApplication extends Application {
  public static HttpUtils.SSLParams sslParams;
        @Override
        public void onCreate() {
            super.onCreate();
            sslParams = HttpUtils.getSslSocketFactory(new InputStream[]{
                    getResources().openRawResource(R.raw.inpet)}, null, null);
    
        }
    }

接著就是配置你請(qǐng)求枯芬,Retrofit 其實(shí)就是常規(guī)配置,不做講解,主要是okhttp的配置

// 證書文件放在了res/raw/目錄下
HttpUtils.SSLParams sslParams = HttpUtils.getSslSocketFactory(new InputStream[]{
                   context.getResources().openRawResource(R.raw.inpet)}, null, null);
// 接下來給okhttp綁定證書
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).build();

到此就已經(jīng)成功搞定了Retrofit 2.0 https的請(qǐng)求

可能會(huì)有小伙伴本地測(cè)試豁鲤,服務(wù)器給你證書HostName不對(duì)或链,返回域名驗(yàn)證不過惫恼,報(bào)錯(cuò)

Android OKHttp https java.io.IOException: Hostname was not verified

別慌,現(xiàn)在有兩種解決辦法:
1.把服務(wù)器端打一頓澳盐,然后讓他重新生成服務(wù)器的證書祈纯,用真實(shí)的本地域名信息
2.不理那個(gè)菜逼,自己解決叼耙,辦法如下:

// 只需要在build多加一個(gè)選項(xiàng)腕窥,verify默認(rèn)返回的是false,只要強(qiáng)制返回true 搞定
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
       .hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    //強(qiáng)行返回true,忽略HostName驗(yàn)證 即驗(yàn)證成功
                    return true;
                }
            }).build();

到此問題已完美解決I竿瘛4乇!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末爽撒,一起剝皮案震驚了整個(gè)濱河市入蛆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌硕勿,老刑警劉巖哨毁,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異首尼,居然都是意外死亡挑庶,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門软能,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迎捺,“玉大人,你說我怎么就攤上這事查排〉手Γ” “怎么了?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵跋核,是天一觀的道長(zhǎng)岖瑰。 經(jīng)常有香客問我,道長(zhǎng)砂代,這世上最難降的妖魔是什么蹋订? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮刻伊,結(jié)果婚禮上露戒,老公的妹妹穿的比我還像新娘椒功。我一直安慰自己,他們只是感情好智什,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布动漾。 她就那樣靜靜地躺著,像睡著了一般荠锭。 火紅的嫁衣襯著肌膚如雪旱眯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天证九,我揣著相機(jī)與錄音删豺,去河邊找鬼。 笑死甫贯,一個(gè)胖子當(dāng)著我的面吹牛吼鳞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播叫搁,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼赔桌,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了渴逻?” 一聲冷哼從身側(cè)響起疾党,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎惨奕,沒想到半個(gè)月后雪位,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡梨撞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年雹洗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卧波。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡时肿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出港粱,到底是詐尸還是另有隱情螃成,我是刑警寧澤,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布查坪,位于F島的核電站寸宏,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏偿曙。R本人自食惡果不足惜氮凝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望望忆。 院中可真熱鬧覆醇,春花似錦朵纷、人聲如沸炭臭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鞋仍。三九已至常摧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間威创,已是汗流浹背落午。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肚豺,地道東北人溃斋。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像吸申,于是被迫代替她去往敵國(guó)和親梗劫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理截碴,服務(wù)發(fā)現(xiàn)梳侨,斷路器,智...
    卡卡羅2017閱讀 134,599評(píng)論 18 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,510評(píng)論 25 707
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,748評(píng)論 6 342
  • 前陣子看到圈子里Retrofit 2.0,RxJava(Android), OkHttp3.3 ,加之支持And...
    Tamic閱讀 44,244評(píng)論 45 208
  • 英語GG 然而觀音靈簽說是先兇后吉哲虾。老實(shí)說丙躏,這個(gè)其實(shí)有點(diǎn)準(zhǔn).. 沒有假期的孩子該何去何從? 好煩惱啊束凑。但是我猜晒旅,六...
    逢茶君閱讀 194評(píng)論 0 1