weex 不支持HTTPS

在android上
擴展一個Adapter,繼承DefaultWXHttpAdapter

public class BingoWXHttpAdapter extends DefaultWXHttpAdapter {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        HttpURLConnection conn = null;
        if (url.getProtocol().toLowerCase().equals("https")) {
            trustAllHosts();
            HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
            httpsCon.setHostnameVerifier(DO_NOT_VERIFY);
            conn = httpsCon;
        } else {
            conn = (HttpURLConnection) url.openConnection();
        }
        return conn;
    }

    //host不驗證
    private HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    //信任所有證書
    private static void trustAllHosts() {
        final String TAG = "trustAllHosts";
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                Log.i(TAG, "checkClientTrusted");
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                Log.i(TAG, "checkServerTrusted");
            }
        }};

        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

之後在初始化Engine的時候註冊它:

setHttpAdapter(new BingoWXHttpAdapter())


 

在iOS上
繼承WXResourceRequestHandlerDefaultImpl實現didReceiveChallenge,具體代碼如下:

#import <Foundation/Foundation.h>
#import "WXResourceRequestHandlerDefaultImpl.h"
#import "WXResourceRequestHandler.h"

@interface BingoWXNetworkImpl : WXResourceRequestHandlerDefaultImpl <WXResourceRequestHandler,NSURLSessionDataDelegate>

@end

#import "BingoWXNetworkImpl.h"

@implementation BingoWXNetworkImpl

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
    
    //    NSURLSessionAuthChallengeUseCredential = 0, 使用(信任)證書
    //    NSURLSessionAuthChallengePerformDefaultHandling = 1, 默認,忽略
    //    NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2,   取消
    //    NSURLSessionAuthChallengeRejectProtectionSpace = 3,      這次取消,下載次還來問
    
    NSLog(@"%@>>>>>>>>>>>>>>>>>>>>>>>>",challenge.protectionSpace);
    // 如果是請求證書信任,我們再來處理,其他的不需要處理
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 調用block
        completionHandler(NSURLSessionAuthChallengeUseCredential,cre);
    }
}
@end


 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章