HarmonyOS鸿蒙Next下,使用axios如何完成证书校验?
HarmonyOS鸿蒙Next下,使用axios如何完成证书校验?
axios<string, AxiosResponse<user>, null>({ url: ‘https://xx.xx.com.cn/auth/xx’, method: ‘post’, caPath: ‘…/…/resources/rawfile/https_server.cer’, //ca证书路径 clientCert: { certPath: ‘…/…/resources/rawfile/https_client.cer’, //客户端证书路径 certType: ‘pem’, // 客户端证书类型,包括pem、der、p12三种 keyPath: ‘…/…/resources/rawfile/https_client_key.pem’, //客户端私钥路径 keyPasswd: ‘123456’ // 密码 } }).then((res: AxiosResponse) => { logger.debug(this.TAG, “httpMain: Success. duration=” + res.data); // }).catch((err: AxiosError) => { // logger.debug(this.TAG, “httpMain: err. =” + err); })
代码如上
报错是:AxiosError: {“code”:2300058,“message”:“Problem with the local SSL certificate”}
请问哪里出错了?
第三方的demo build失败。
更多关于HarmonyOS鸿蒙Next下,使用axios如何完成证书校验?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你好,请问这个功能实现了吗
更多关于HarmonyOS鸿蒙Next下,使用axios如何完成证书校验?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
现在问题是,我们的服务器的证书校验报风险,这种情况下,是不是鸿蒙不支持请求?
总的来说,HarmonyOS是一款非常优秀的操作系统,期待它能在未来带给我们更多惊喜!
可以在证书文件中配置多个证书,默认证书/etc/ssl/certs/cacert.pem
中就有很多证书。
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17
你好,想问一下,只需在对应的文件夹下存放证书文件就行了吗?还需要别的代码实现吗?
在HarmonyOS鸿蒙Next下,使用axios
进行证书校验,可以通过配置httpsAgent
来实现。具体步骤如下:
-
准备证书文件:首先,确保你拥有服务器端提供的CA证书文件(通常为
.crt
或.pem
格式)。 -
创建
httpsAgent
:使用https
模块创建一个https.Agent
实例,并将证书文件加载到该实例中。可以通过fs
模块读取证书文件,然后使用https.createAgent
方法创建httpsAgent
。 -
配置
axios
:在axios
请求中,将httpsAgent
配置为请求的httpsAgent
参数。
以下是示例代码:
const https = require('https');
const fs = require('fs');
const axios = require('axios');
// 读取CA证书文件
const caCert = fs.readFileSync('/path/to/your/ca.crt');
// 创建httpsAgent
const httpsAgent = new https.Agent({
ca: caCert
});
// 配置axios请求
axios.get('https://your-api-endpoint.com/data', {
httpsAgent: httpsAgent
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在这个示例中,httpsAgent
会使用提供的CA证书进行服务器证书的校验。如果服务器证书与CA证书不匹配,请求将会失败。
请注意,证书文件的路径需要根据实际情况进行调整。此方法适用于需要在HarmonyOS鸿蒙Next下进行严格证书校验的场景。
在HarmonyOS鸿蒙Next中使用axios
进行证书校验,可以通过配置httpsAgent
来实现。首先,使用https
模块创建一个Agent
,并加载你的证书文件。然后,在axios
请求中设置httpsAgent
。以下是一个示例代码:
const axios = require('axios');
const https = require('https');
const fs = require('fs');
// 加载证书
const httpsAgent = new https.Agent({
ca: fs.readFileSync('path/to/your/certificate.pem')
});
// 发起请求
axios.get('https://your-api-endpoint', {
httpsAgent
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
确保将path/to/your/certificate.pem
替换为你的证书文件路径。这样,axios
在发起请求时会验证服务器证书的有效性。