欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

weex https 设置

程序员文章站 2024-03-24 18:28:34
...

在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

weex https 设置
weex https 设置

更多详解:
喜欢可以加@群号:913934649

简书: https://www.jianshu.com/u/88db5f15770d

csdn:https://me.csdn.net/beyondforme

掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7