错误javax.net.ssl.SSLHandshakeException:

时间:2018-08-15 14:01:15

标签: java ssl https certificate nativescript

我真的需要您的支持。

我有一个大问题,我不明白为什么会发生。

我使用https-nativescrip t插件是因为我想与协议https通信。

因此,首先,我安装此插件,并在component.ts中编写以下代码:

enableSSLpinning() {
    let certificate: any;
    Https.enableSSLPinning({ host: 'xx.xxx.xx.xx:3333', certificate, allowInvalidCertificates: true, validatesDomainName: false })
    Https.request({
        url: 'https://xx.xxx.xx.xx:3333/user',
        method: 'GET',
        headers: {
            "Content-type": "application/x-www-form-urlencoded",
        },
    }).then(function (response) {
        console.log('Https.request response', response);
    }).catch(function (error) {
        console.error('Https.request error', error);
    })
}

在https.android.js中,我仅在此部分修改证书:

  function enableSSLPinning(options) {
         if (!peer.host && !peer.certificate
               ) {
               var certificate = void 0;
             var InputSteram = void 0;
    try {
        var inputStream = new java.io.ByteArrayInputStream(new java.lang.String("-----BEGIN CERTIFICATE-----\n"
            + "MIIFjDCCA3SgAwIBAgIJAMOXpEn+QQSVMA0GCSqGSIb3DQEBCwUAMIGBMQswCQYD\n"
            + "VQQGEwJVUzELMAkGA1UECAwCTUExDzANBgNVBAcMBkJvc3RvbjETMBEGA1UECgwK\n"
              ..................
            + "1AYJwo2yFqmetdmOYaFh6Cli8OerUERDqPB1UKPmYQE=\n"
            + "-----END CERTIFICATE-----").getBytes("UTF-8"));
        var x509Certificate = java.security.cert.CertificateFactory.getInstance('X.509').generateCertificate(inputStream);
        peer.x509Certificate = x509Certificate;
        certificate = okhttp3.CertificatePinner.pin(x509Certificate);
        inputStream.close();
        }
    catch (error) {
        try {
            if (inputStream) {
                console.log('inputStream', inputStream)
                inputStream.close();
            }
        }
        catch (e) { }
        console.error('nativescript-https > enableSSLPinning error', error);
        return;
    }
    peer.host = options.host;
    peer.certificate = certificate;
    if (options.allowInvalidCertificates == true) {
        peer.allowInvalidCertificates = true;
    }
    if (options.validatesDomainName == false) {
        peer.validatesDomainName = false;
    }
}
peer.enabled = true;
getClient(true);
console.log('nativescript-https > Enabled SSL pinning');

}

这部分执行正确,以控制台打印'nativescript-https > Enabled SSL pinning'

此部分显示错误:console.error('Https.request error', error);

  

JS:Https.request错误javax.net.ssl.SSLHandshakeException:   java.security.cert.CertPathValidatorException:的信任锚   找不到认证路径。

然后在https.android.js中调用此函数

function request(opts) {
    console.log('opts', opts)
    return new Promise(function (resolve, reject) {
        try {
            var client = getClient();
            var request_1 = new okhttp3.Request.Builder();
            request_1.url(opts.url);
            var reqheads_1 = opts.headers;
            Object.keys(reqheads_1).forEach(function (key) {
                request_1.addHeader(key, reqheads_1[key]);
            });
            if (opts.method == 'GET') {
                request_1.get();
            }
            else if (opts.method == 'POST') {
                var type = okhttp3.MediaType.parse('application/json');
                var body = okhttp3.RequestBody.create(type, opts.content);
                request_1.post(body);
            }
            client.newCall(request_1.build()).enqueue(new okhttp3.Callback({
                onResponse: function (task, response) {
                    var content;
                    try {
                        content = JSON.parse(response.body().string());
                    }
                    catch (error) {
                        return reject(error);
                    }
                    var statusCode = response.code();
                    var headers = {};
                    var heads = response.headers();
                    var i, len = heads.size();
                    for (i = 0; i < len; i++) {
                        var key = heads.name(i);
                        var value = heads.value(i);
                        headers[key] = value;
                    }
                    resolve({ content: content, statusCode: statusCode, headers: headers });
                },
                onFailure: function (task, error) {
                    reject(error);
                },
            }));
        }
        catch (error) {
            reject(error);
        }
    });
}

请,您能问我任何想法吗,这是我的代码中的问题?谢谢

谢谢!

更新:

我认为这部分代码存在问题:

  onResponse: function (task, response) {
            console.load('testfdsfsdfsdfsd')
            var content;
            console.log('content', content)
            try {
                content = JSON.parse(response.body().string());
                console.log('content1', content)
            }
            catch (error) {
                console.log('error111111', error)
                return reject(error);
            }
            var statusCode = response.code();
            var headers = {};
            var heads = response.headers();
            var i, len = heads.size();
            for (i = 0; i < len; i++) {
                var key = heads.name(i);
                var value = heads.value(i);
                headers[key] = value;
            }
            resolve({ content: content, statusCode: statusCode, headers: headers });
        },

由于未执行此部分,因此直接传递给onFailure

onFailure: function (task, error) {
                reject(error);
            },

0 个答案:

没有答案