开发微信小程序中SSL协议的申请、证书绑定、TLS 版本处理等
在上篇隨筆《基于微信小程序的系統(tǒng)開發(fā)準(zhǔn)備工作》介紹了開發(fā)微信小程序的一些前期的架構(gòu)設(shè)計(jì)、技術(shù)路線?、工具準(zhǔn)備等方面內(nèi)容,本篇隨筆繼續(xù)這個(gè)步驟,逐步介紹我們實(shí)際開發(fā)過程中對(duì)SSL協(xié)議的申請(qǐng)及后期處理過程,包括證書的IIS端口綁定,以及解決“”小程序要求的 TLS 版本必須大于等于 1” 的問題。
1、證書申請(qǐng)及成功的后續(xù)處理
小程序的配置要求我們必須在網(wǎng)站提供443端口服務(wù),需要加入HTTPS證書,這種證書可以從云服務(wù)商上購買,如騰訊云、阿里云上的云服務(wù)器后臺(tái)都提供了購買證書服務(wù)的通道,以阿里云為例,使用阿里云賬號(hào)登陸后,在【控制臺(tái)】【安全云盾】【證書服務(wù)】里面進(jìn)行申請(qǐng)。
購買證書,我們?cè)跊]有太多資金支持的情況下,可以考慮先使用免費(fèi)SSL證書,阿里云上提供?免費(fèi)型DV SSL的申請(qǐng),購買后,會(huì)在訂單列表里面有一個(gè)待審核的訂單,如下所示,等待審核通過即可使用。
一般情況下,如果我們填寫的資料正確,會(huì)較快通過審核的,如果我們的DNS不在萬網(wǎng)上,那么我們還需要到服務(wù)商的網(wǎng)站進(jìn)行添加阿里云的DNS配置。通過我們?cè)谔峤恍畔⒌臅r(shí)候,如果是Windows服務(wù)器,因此會(huì)勾選DNS方式驗(yàn)證,如下所示。
這樣提交成功后,會(huì)同時(shí)在服務(wù)器的云解析上面自動(dòng)增加一條記錄,如下所示
如果我們的申請(qǐng)的免費(fèi)SSL證書獲得通過,那么狀態(tài)會(huì)變?yōu)椤疽押灠l(fā)】,如下所示,這個(gè)時(shí)候就可以用了。
?下載的證書包括幾個(gè)文件,如下所示。
我們?cè)贗IS服務(wù)器上雙擊pfx文件,默認(rèn)一步步操作即可把證書增加加到對(duì)應(yīng)的目錄里面了。
接著我們可以在控制臺(tái)中查看到對(duì)應(yīng)的證書位置。
然后在IIS里面綁定443端口,選擇對(duì)應(yīng)的SSL證書即可完成對(duì)SSL證書的綁定了,如下圖所示。
這個(gè)時(shí)候,如果我們?cè)L問網(wǎng)站(我們官網(wǎng)是https://www.iqidi.com),那么?就可以在瀏覽器的左側(cè)看到證書的信息了。
?
2、微信小程序整合處理
為了整合遠(yuǎn)程HTTPS連接獲取數(shù)據(jù),我們需要進(jìn)行部署一個(gè)Web?API的接口,那么我們可以建立一個(gè)進(jìn)行MVC控制器進(jìn)行測(cè)試,如下我們?cè)诳刂破骼锩嫣砑右粋€(gè)方法來獲取第三方接口的數(shù)據(jù),然后返回來給我們的小程序使用。
例如,我們以連接地址:http://m.maoyan.com/movie/list.json返回的數(shù)據(jù)為例,這個(gè)接口用來獲取電影的數(shù)據(jù),獲得的結(jié)果如下所示。
由于小程序?qū)τ蛎南拗?#xff0c;我們不能使用第三方的API接口,因此需要在自己域名內(nèi)部的API進(jìn)行封裝處理,然后再提供給自己的小程序使用,我們建立一個(gè)MVC的控制器方法,如下代碼所示。
/// <summary>/// 增加一個(gè)域名內(nèi)的接口,供小程序測(cè)試/// </summary>/// <returns></returns> [HttpPost]public ActionResult Movies(int offset = 0, string type = "hot", int limit=6){var url = "http://m.maoyan.com/movie/list.json";var postData = string.Format("?offset={0}&type={1}&limit={2}", offset,type,limit);HttpHelper helper = new HttpHelper();string result = helper.GetHtml(url postData, "", false);return Content(result);}這樣我們使用Fiddler測(cè)試的時(shí)候,確信能夠獲得返回的JSON數(shù)據(jù),在進(jìn)行小程序的測(cè)試即可。
執(zhí)行POST數(shù)據(jù)的處理,可以獲得對(duì)應(yīng)的JSON數(shù)據(jù),如下所示。
不過如果我們這個(gè)時(shí)候整合小程序進(jìn)行測(cè)試,如下代碼所示。
onShow: function () {var that = thiswx.request({url: 'https://www.iqidi.com/h5/movies',data: {offset: 0,type: 'hot',limit: that.data.limit},method : 'POST',header: {'Content-Type': 'application/json'},success: function (res) {console.log(res.data)that.setData({films: res.data.data.movies,loading: true})}})那么上述的處理操作,還是沒有能夠獲取正確的結(jié)果的,調(diào)試小程序發(fā)現(xiàn),它提示”小程序要求的 TLS 版本必須大于等于 1.2“”。
?
在網(wǎng)站上找到對(duì)應(yīng)的解決方案,測(cè)試后正確解決問題:在?PowerShell中運(yùn)行以下內(nèi)容, 然后重啟服務(wù)器
# Enables TLS 1.2 on windows Server 2008 R2 and Windows 7# These keys do not exist so they need to be created prior to setting values. md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client"# Enable TLS 1.2 for client and server SCHANNEL communications new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "DisabledByDefault" -value 0 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "DisabledByDefault" -value 0 -PropertyType "DWord"# Disable SSL 2.0 (PCI Compliance) md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" -name Enabled -value 0 -PropertyType "DWord" # Enables TLS 1.2 on Windows Server 2008 R2 and Windows 7 # These keys do not exist so they need to be created prior to setting values. md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" # Enable TLS 1.2 for client and server SCHANNEL communications new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "DisabledByDefault" -value 0 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "DisabledByDefault" -value 0 -PropertyType "DWord" # Disable SSL 2.0 (PCI Compliance) md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" -name Enabled -value 0 -PropertyType "DWord"這樣啟動(dòng)接口,獲得數(shù)據(jù)并在小程序中進(jìn)行正確展示了。
?
更多專業(yè)前端知識(shí),請(qǐng)上 【猿2048】www.mk2048.com
總結(jié)
以上是生活随笔為你收集整理的开发微信小程序中SSL协议的申请、证书绑定、TLS 版本处理等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css布局笔记(二)Flex
- 下一篇: MapXtreme2008中操作矢量符号