欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Https證書過期的兩種解決方案

 更新時(shí)間:2022年12月24日 09:50:55   作者:xiangzhihong8  
應(yīng)該有很多小伙伴遇到這樣一個(gè)問題,在線上已發(fā)布的app里,關(guān)于https的cer證書過期,從而導(dǎo)致app所有網(wǎng)絡(luò)請(qǐng)求失效無法使用,這篇文章主要介紹了Android Https證書過期的解決方案,需要的朋友可以參考下

應(yīng)該有很多小伙伴遇到這樣一個(gè)問題,在線上已發(fā)布的app里,關(guān)于https的cer證書過期,從而導(dǎo)致app所有網(wǎng)絡(luò)請(qǐng)求失效無法使用。

這個(gè)時(shí)候有人就要說了,應(yīng)急發(fā)布一個(gè)已更新最新cer證書的apk不就完事了么,其實(shí)沒那么簡單,iOS還好可以通過appstore提供的api查詢到新版本,但android就不一樣了,需要調(diào)用自己Server端提供的api接口查詢到新版本,并獲取apk下載路徑,問題是https都不能訪問了,如何請(qǐng)求到版本信息呢?下面提供兩種常見的解決方案:

方案一

將版本信息接口讓后臺(tái)改成http(不推薦,后臺(tái)因素不可控),或者將本地https的設(shè)置一個(gè)不安全校驗(yàn)(推薦)。

private static OkHttpClient newOkHttpClient(int timeout){
 
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
 
        return new OkHttpClient.Builder()
                .addInterceptor(new RequestInfoInterceptor())
                //.addInterceptor(logging)
                .addNetworkInterceptor(new TokenHeaderInterceptor())
                .sslSocketFactory(Certificate.getSSLSocketFactory())
                //設(shè)置不安全校驗(yàn)
                .hostnameVerifier(Certificate.getUnSafeHostnameVerifier())
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .build();
    }
 
    /**
     *獲取HostnameVerifier 
     */
    public static HostnameVerifier getUnSafeHostnameVerifier() {
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        return hostnameVerifier;
    }

方案二

將xxx.cer證書改成動(dòng)態(tài)讀取(以文件的方式從app沙盒里面讀取即可),在https證書即將過期時(shí),從服務(wù)器下載最新的cer證書更新到沙盒里面,App每次初始化網(wǎng)絡(luò)請(qǐng)求時(shí)讀取sdcard最新的證書文件,這樣App就永遠(yuǎn)不會(huì)出現(xiàn)https證書過期導(dǎo)致無法使用的問題,流程圖如下。

在這里插入圖片描述

下面是一些關(guān)鍵的代碼:

    private static OkHttpClient newOkHttpClient(int timeout){
 
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
 
        return new OkHttpClient.Builder()
                .addInterceptor(new RequestInfoInterceptor())
                //.addInterceptor(logging)
                .addNetworkInterceptor(new TokenHeaderInterceptor())
                .sslSocketFactory(Certificate.getSSLSocketFactory(BaseApplcation.myApp, new String[]{"/sdcard/xxx.cer"}))
                .hostnameVerifier(Certificate.getUnSafeHostnameVerifier())
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .build();
    }
  
    /**
     * 帶證書的,從本地文件讀取
     * @param context
     * @param certificatesFiles  本地文件(通過下載到本地)
     * @return
     */
    public static SSLSocketFactory getSSLSocketFactory(Context context, String[] certificatesFiles) {
        if (context == null) {
            throw new NullPointerException("context == null");
        }
        CertificateFactory certificateFactory;
        try {
            certificateFactory = CertificateFactory.getInstance("X.509");
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(null, null);
 
            for (int i = 0; i < certificatesFiles.length; i++) {
                InputStream certificate = new FileInputStream(certificatesFiles[i]);
                keyStore.setCertificateEntry(String.valueOf(i), certificateFactory.generateCertificate(certificate));
 
                if (certificate != null) {
                    certificate.close();
                }
            }
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
 
        }
        return null;
    }
 
  /**
     * 帶證書的,從raw資源中讀取
     * @param context
     * @param certificates  rawIds
     * @return
     */
    public static SSLSocketFactory getSSLSocketFactory(Context context, int[] certificates) {
        if (context == null) {
            throw new NullPointerException("context == null");
        }
        CertificateFactory certificateFactory;
        try {
            certificateFactory = CertificateFactory.getInstance("X.509");
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(null, null);
 
            for (int i = 0; i < certificates.length; i++) {
                InputStream certificate = context.getResources().openRawResource(certificates[i]);
                keyStore.setCertificateEntry(String.valueOf(i), certificateFactory.generateCertificate(certificate));
 
                if (certificate != null) {
                    certificate.close();
                }
            }
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
 
        }
        return null;
    }

到此這篇關(guān)于Android Https證書過期的解決方案的文章就介紹到這了,更多相關(guān)Android Https證書過期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flutter路由框架Fluro使用簡介

    Flutter路由框架Fluro使用簡介

    這篇文章主要介紹了Flutter路由框架Fluro使用簡介,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Android 實(shí)現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能

    Android 實(shí)現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能

    這篇文章主要介紹了Android 實(shí)現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android開發(fā)flow常見API的使用示例詳解

    Android開發(fā)flow常見API的使用示例詳解

    這篇文章主要為大家介紹了Android開發(fā)flow常見API的使用示例詳解,希望能夠幫助大家更好的掌握flow使用,熟練的應(yīng)用于各種場景,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android實(shí)現(xiàn)固定屏幕顯示的方法

    Android實(shí)現(xiàn)固定屏幕顯示的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)固定屏幕顯示的方法,實(shí)例分析了Android屏幕固定顯示所涉及的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Android Handler內(nèi)存泄漏詳解及其解決方案

    Android Handler內(nèi)存泄漏詳解及其解決方案

    在android開發(fā)過程中,我們可能會(huì)遇到過令人奔潰的OOM異常,這篇文章主要介紹了Android Handler內(nèi)存泄漏詳解及其解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 解決Android 6.0獲取wifi Mac地址為02:00:00:00:00:00問題

    解決Android 6.0獲取wifi Mac地址為02:00:00:00:00:00問題

    這篇文章主要介紹了Android 6.0獲取wifi Mac地址為02:00:00:00:00:00的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • android ViewPager實(shí)現(xiàn)滑動(dòng)翻頁效果實(shí)例代碼

    android ViewPager實(shí)現(xiàn)滑動(dòng)翻頁效果實(shí)例代碼

    本篇文章主要介紹了android ViewPager實(shí)現(xiàn)滑動(dòng)翻頁效果實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android Force Close 出現(xiàn)的異常原因分析及解決方法

    Android Force Close 出現(xiàn)的異常原因分析及解決方法

    本文給大家講解Android Force Close 出現(xiàn)的異常原因分析及解決方法,forceclose意為強(qiáng)行關(guān)閉,當(dāng)前應(yīng)用程序發(fā)生了沖突。對(duì)android force close異常分析感興趣的朋友一起通過本文學(xué)習(xí)吧
    2016-08-08
  • 詳解JS與APP原生控件交互

    詳解JS與APP原生控件交互

    本文主要分享了JavaScript與Android、IOS原生控件之間相互通信的詳細(xì)代碼實(shí)現(xiàn),具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • Android 8.0系統(tǒng)中通知欄的適配詳解

    Android 8.0系統(tǒng)中通知欄的適配詳解

    本片文章給大家通過實(shí)例講解分析了Android 8.0系統(tǒng)中通知欄的相關(guān)知識(shí)點(diǎn),對(duì)此有需要的朋友可以參考學(xué)習(xí)下。
    2018-04-04

最新評(píng)論