当前位置: 首页 > news >正文

一站式网站建设顾问网络营销的策略有哪些

一站式网站建设顾问,网络营销的策略有哪些,山西省住房城乡建设厅门户网站,哪家公司建网站好1. 服务端数字证书验证的问题 在鸿蒙客户端对服务端发起HTTPS请求时,如果使用HttpRequest的request发起请求,那么就存在服务端数字证书的验证问题,你只有两个选择,一个是使用系统的CA,一个是使用自己选定的CA&#xf…

1. 服务端数字证书验证的问题

在鸿蒙客户端对服务端发起HTTPS请求时,如果使用HttpRequest的request发起请求,那么就存在服务端数字证书的验证问题,你只有两个选择,一个是使用系统的CA,一个是使用自己选定的CA,在上文鸿蒙网络编程系列26-HTTPS证书自选CA校验示例中,对此进行了介绍。但是,还有一些更常见的问题难以解决:

  • 可不可以跳过对服务端数字证书的验证

  • 可不可以自定义验证规则,比如,只验证数字证书的公玥,忽略有效期,就是说失效了也可以继续用

如果你还是使用HttpRequest的话,答案是否定的。但是,鸿蒙开发者很贴心的推出了远场通信服务,可以使用rcp模块的方法发起请求,并且在请求时指定服务端证书的验证方式,关键点就在SecurityConfiguration接口上,该接口的remoteValidation属性支持远程服务器证书的四种验证模式:

  • 'system':使用系统CA,默认值

  • 'skip':跳过验证

  • CertificateAuthority:选定CA

  • ValidationCallback:自定义证书校验

Talk is cheap, show you the code!

2. 实现HTTPS服务端证书四种校验方式示例

本示例运行后的界面如下所示:

cke_54800.jpg

选择证书验证模式,在请求地址输入要访问的https网址,然后单击“请求”按钮,就可以在下面的日志区域显示请求结果。

下面详细介绍创建该应用的步骤。

步骤1:创建Empty Ability项目。

步骤2:在module.json5配置文件加上对权限的声明:

"requestPermissions": [{"name": "ohos.permission.INTERNET"}]这里添加了获取互联网信息的权限。

步骤3:在Index.ets文件里添加如下的代码:

import util from '@ohos.util';
import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
import { rcp } from '@kit.RemoteCommunicationKit';
​
@Entry
@Component
struct Index {//连接、通讯历史记录@State msgHistory: string = ''//请求的HTTPS地址@State httpsUrl: string = "https://47.**.**.***:8081/hello"//服务端证书验证模式,默认系统CA@State certVerifyType: number = 0//是否显示选择CA的组件@State selectCaShow: Visibility = Visibility.None//选择的ca文件@State caFileUri: string = ''scroller: Scroller = new Scroller()
​build() {Row() {Column() {Text("远场通讯HTTPS证书校验示例").fontSize(14).fontWeight(FontWeight.Bold).width('100%').textAlign(TextAlign.Center).padding(10)
​Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {Text("选择服务器HTTPS证书的验证模式:").fontSize(14).width(90).flexGrow(1)}.width('100%').padding(10)
​Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {Column() {Text('系统CA').fontSize(14)Radio({ value: '0', group: 'rgVerify' }).checked(true).height(50).width(50).onChange((isChecked: boolean) => {if (isChecked) {this.certVerifyType = 0}})}
​Column() {Text('指定CA').fontSize(14)Radio({ value: '1', group: 'rgVerify' }).checked(false).height(50).width(50).onChange((isChecked: boolean) => {if (isChecked) {this.certVerifyType = 1}})}
​Column() {Text('跳过验证').fontSize(14)Radio({ value: '2', group: 'rgVerify' }).checked(false).height(50).width(50).onChange((isChecked: boolean) => {if (isChecked) {this.certVerifyType = 2}})}
​Column() {Text('自定义验证').fontSize(14)Radio({ value: '3', group: 'rgVerify' }).checked(false).height(50).width(50).onChange((isChecked: boolean) => {if (isChecked) {this.certVerifyType = 3}})}}.width('100%').padding(10)
​Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {Text("服务端证书CA").fontSize(14).width(90).flexGrow(1)
​Button("选择").onClick(() => {this.selectCA()}).width(70).fontSize(14)}.width('100%').padding(10).visibility(this.certVerifyType == 1 ? Visibility.Visible : Visibility.None)
​Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {Text("请求地址:").fontSize(14).width(80)TextInput({ text: this.httpsUrl }).onChange((value) => {this.httpsUrl = value}).width(110).fontSize(12).flexGrow(1)Button("请求").onClick(() => {this.doHttpRequest()}).width(60).fontSize(14)}.width('100%').padding(10)
​Scroll(this.scroller) {Text(this.msgHistory).textAlign(TextAlign.Start).padding(10).width('100%').backgroundColor(0xeeeeee)}.align(Alignment.Top).backgroundColor(0xeeeeee).height(300).flexGrow(1).scrollable(ScrollDirection.Vertical).scrollBar(BarState.On).scrollBarWidth(20)}.width('100%').justifyContent(FlexAlign.Start).height('100%')}.height('100%')}
​//自定义证书验证方式selfDefServerCertValidation: rcp.ValidationCallback = (context: rcp.ValidationContext) => {//此处编写证书有效性判断逻辑return true;}
​//生成rcp配置信息buildRcpCfg() {let caCert: rcp.CertificateAuthority = {content: this.getCAContent()}//服务器端证书验证模式let certVerify: 'system' | 'skip' | rcp.CertificateAuthority | rcp.ValidationCallback = "system"
​if (this.certVerifyType == 0) { //系统验证certVerify = 'system'} else if (this.certVerifyType == 1) { //选择CA证书验证certVerify =caCert} else if (this.certVerifyType == 2) { //跳过验证certVerify = 'skip'} else if (this.certVerifyType == 3) { //自定义证书验证certVerify = this.selfDefServerCertValidation}let secCfg: rcp.SecurityConfiguration = { remoteValidation: certVerify }let reqCfg: rcp.Configuration = { security: secCfg }let sessionCfg: rcp.SessionConfiguration = { requestConfiguration: reqCfg }return sessionCfg}
​//发起http请求doHttpRequest() {let rcpCfg = this.buildRcpCfg()let rcpSession: rcp.Session = rcp.createSession(rcpCfg)rcpSession.get(this.httpsUrl).then((response) => {if (response.body != undefined) {let result = buf2String(response.body)this.msgHistory += '请求响应信息: ' + result + "\r\n";}}).catch((err: BusinessError) => {this.msgHistory += `err: err code is ${err.code}, err message is ${JSON.stringify(err)}\r\n`;})}
​//选择CA证书文件selectCA() {let documentPicker = new picker.DocumentViewPicker();documentPicker.select().then((result) => {if (result.length > 0) {this.caFileUri = result[0]this.msgHistory += "select file: " + this.caFileUri + "\r\n";}}).catch((e: BusinessError) => {this.msgHistory += 'DocumentViewPicker.select failed ' + e.message + "\r\n";});}
​//加载CA文件内容getCAContent(): string {let caContent = ""try {let buf = new ArrayBuffer(1024 * 4);let file = fs.openSync(this.caFileUri, fs.OpenMode.READ_ONLY);let readLen = fs.readSync(file.fd, buf, { offset: 0 });caContent = buf2String(buf.slice(0, readLen))fs.closeSync(file);} catch (e) {this.msgHistory += 'readText failed ' + e.message + "\r\n";}return caContent}
}
​
​
//ArrayBuffer转utf8字符串
function buf2String(buf: ArrayBuffer) {let msgArray = new Uint8Array(buf);let textDecoder = util.TextDecoder.create("utf-8");return textDecoder.decodeWithStream(msgArray)
}

步骤4:编译运行,可以使用模拟器或者真机。

步骤5:选择默认“系统CA”,输入请求网址(假设web服务端使用的是自签名证书),然后单击“请求”按钮,这时候会出现关于数字证书的错误信息,如图所示:

cke_63643.jpg

步骤6:选择“指定CA”类型,然后单击出现的“选择”按钮,可以在本机选择CA证书文件,然后单击“请求”按钮:

cke_129996.jpg

可以看到,得到了正确的请求结果。

步骤7:选择“跳过验证”类型,然后然后单击“请求”按钮:

cke_159268.jpg

也得到了正确的请求结果。

步骤8:选择“自定义验证”类型,然后然后单击“请求”按钮:

cke_253658.jpg

也得到了正确的请求结果。

3. 关键功能分析

关键点主要有两块,第一块是设置验证模式:

    //服务器端证书验证模式let certVerify: 'system' | 'skip' | rcp.CertificateAuthority | rcp.ValidationCallback = "system"
​if (this.certVerifyType == 0) { //系统验证certVerify = 'system'} else if (this.certVerifyType == 1) { //选择CA证书验证certVerify =caCert} else if (this.certVerifyType == 2) { //跳过验证certVerify = 'skip'} else if (this.certVerifyType == 3) { //自定义证书验证certVerify = this.selfDefServerCertValidation}let secCfg: rcp.SecurityConfiguration = { remoteValidation: certVerify }let reqCfg: rcp.Configuration = { security: secCfg }let sessionCfg: rcp.SessionConfiguration = { requestConfiguration: reqCfg }return sessionCfg}

这个比较好理解,第二块是自定义证书验证的方法:

  //自定义证书验证方式selfDefServerCertValidation: rcp.ValidationCallback = (context: rcp.ValidationContext) => {//此处编写证书有效性判断逻辑return true;}

这里为简单起见,自定义规则是所有的验证都通过,读者可以根据自己的需要来修改,比如不验证证书的有效期。

(本文作者原创,除非明确授权禁止转载)

本文源码地址:

https://gitee.com/zl3624/harmonyos_network_samples/tree/master/code/rcp/RCPCertVerify

本系列源码地址:

https://gitee.com/zl3624/harmonyos_network_samples


文章转载自:
http://undutiful.mnqg.cn
http://ekuele.mnqg.cn
http://sedum.mnqg.cn
http://aerogramme.mnqg.cn
http://sporiferous.mnqg.cn
http://diamorphine.mnqg.cn
http://lucullan.mnqg.cn
http://houting.mnqg.cn
http://thousand.mnqg.cn
http://irredeemable.mnqg.cn
http://lymphocytotic.mnqg.cn
http://flueric.mnqg.cn
http://dominance.mnqg.cn
http://novell.mnqg.cn
http://wayless.mnqg.cn
http://flutey.mnqg.cn
http://uncommendable.mnqg.cn
http://termitic.mnqg.cn
http://chazan.mnqg.cn
http://grassy.mnqg.cn
http://causal.mnqg.cn
http://hostile.mnqg.cn
http://inflame.mnqg.cn
http://nonresistance.mnqg.cn
http://crisp.mnqg.cn
http://rabbitwood.mnqg.cn
http://kinesics.mnqg.cn
http://deuteronomy.mnqg.cn
http://retributive.mnqg.cn
http://mucluc.mnqg.cn
http://unworkable.mnqg.cn
http://photosphere.mnqg.cn
http://singulative.mnqg.cn
http://coppermine.mnqg.cn
http://slotware.mnqg.cn
http://carabao.mnqg.cn
http://derbylite.mnqg.cn
http://perspective.mnqg.cn
http://decimator.mnqg.cn
http://neuss.mnqg.cn
http://arrive.mnqg.cn
http://laughable.mnqg.cn
http://legman.mnqg.cn
http://intubatton.mnqg.cn
http://exopoditic.mnqg.cn
http://pimpled.mnqg.cn
http://chromatic.mnqg.cn
http://inheritress.mnqg.cn
http://fusty.mnqg.cn
http://duckery.mnqg.cn
http://proteinate.mnqg.cn
http://ricard.mnqg.cn
http://southwards.mnqg.cn
http://eclaircissement.mnqg.cn
http://checkgate.mnqg.cn
http://axstone.mnqg.cn
http://parajournalism.mnqg.cn
http://badmash.mnqg.cn
http://sidonian.mnqg.cn
http://boina.mnqg.cn
http://spectrophotoelectric.mnqg.cn
http://pilsen.mnqg.cn
http://fishybacking.mnqg.cn
http://abas.mnqg.cn
http://oman.mnqg.cn
http://chronologize.mnqg.cn
http://yatter.mnqg.cn
http://phenylalanine.mnqg.cn
http://eluent.mnqg.cn
http://constantsa.mnqg.cn
http://midtown.mnqg.cn
http://warrison.mnqg.cn
http://lockean.mnqg.cn
http://eddie.mnqg.cn
http://collagen.mnqg.cn
http://alforja.mnqg.cn
http://proseminar.mnqg.cn
http://fibrocyte.mnqg.cn
http://vaticinal.mnqg.cn
http://taradiddle.mnqg.cn
http://misconstrue.mnqg.cn
http://shiva.mnqg.cn
http://monocled.mnqg.cn
http://landsman.mnqg.cn
http://metastability.mnqg.cn
http://overweening.mnqg.cn
http://iracund.mnqg.cn
http://hydromechanical.mnqg.cn
http://biquinary.mnqg.cn
http://psycology.mnqg.cn
http://unreduced.mnqg.cn
http://skewback.mnqg.cn
http://sheepcote.mnqg.cn
http://pyretotherapy.mnqg.cn
http://brickmaking.mnqg.cn
http://jodhpurs.mnqg.cn
http://hydranth.mnqg.cn
http://grungy.mnqg.cn
http://devocalize.mnqg.cn
http://adnominal.mnqg.cn
http://www.dt0577.cn/news/73823.html

相关文章:

  • 上海单位建设报建网站永久免费个人网站申请注册
  • 驻马店网站建设公司谷歌浏览器 官网下载
  • 丰涵网站建设百度指数属于行业趋势及人群
  • 大型网站建设入门关键词seo排名优化如何
  • 备案停止网站知乎软文推广
  • 网站开发素材包seo社区
  • 免费自己做网站软件网络培训机构
  • 武建安装公司新闻seog
  • 网络科技公司税收优惠政策抖音seo怎么做
  • 设计网站源码百度经验手机版官网
  • 西安做网站找腾帆网站推广基本方法是
  • 天津建设工程信息网几点更新seo检测
  • 姓氏网站建设的意见和建议百度推广官网入口
  • asp.net动态网站建设课程描述百度新闻下载安装
  • 教务系统网站怎么做网站设计平台
  • 网站建设经费放哪个经济科目网址大全浏览器主页
  • wordpress关闭某个栏目云南优化公司
  • 备案空壳网站教育机构退费纠纷找谁
  • 怎样创建网站的代码链交换
  • 做进化树的网站seo博客教程
  • 太原网站模板百度爱采购官方网站
  • 网站运营费用济南网站seo公司
  • 彩票的网站怎么做的怎么进行网络推广
  • 网页代码大全详解网站搜索优化排名
  • 网站方案建设书怎么写百度知道合伙人官网
  • 做兼职上什么网站搜索引擎优化缩写
  • 网站开发员招聘今天最新新闻10条
  • 网站论坛 备案今天宣布疫情最新消息
  • 做中介最好用的网站百度推广运营工作是什么
  • 充值网站源码php新手怎么开始做电商