开发微信小程序中SSL协议的申请、证书绑定、TLS 版本处理等
在上篇隨筆《基于微信小程序的系統(tǒng)開發(fā)準備工作》介紹了開發(fā)微信小程序的一些前期的架構(gòu)設計、技術路線?、工具準備等方面內(nèi)容,本篇隨筆繼續(xù)這個步驟,逐步介紹我們實際開發(fā)過程中對SSL協(xié)議的申請及后期處理過程,包括證書的IIS端口綁定,以及解決“”小程序要求的 TLS 版本必須大于等于 1” 的問題。
1、證書申請及成功的后續(xù)處理
小程序的配置要求我們必須在網(wǎng)站提供443端口服務,需要加入HTTPS證書,這種證書可以從云服務商上購買,如騰訊云、阿里云上的云服務器后臺都提供了購買證書服務的通道,以阿里云為例,使用阿里云賬號登陸后,在【控制臺】【安全云盾】【證書服務】里面進行申請。
購買證書,我們在沒有太多資金支持的情況下,可以考慮先使用免費SSL證書,阿里云上提供?免費型DV SSL的申請,購買后,會在訂單列表里面有一個待審核的訂單,如下所示,等待審核通過即可使用。
一般情況下,如果我們填寫的資料正確,會較快通過審核的,如果我們的DNS不在萬網(wǎng)上,那么我們還需要到服務商的網(wǎng)站進行添加阿里云的DNS配置。通過我們在提交信息的時候,如果是Windows服務器,因此會勾選DNS方式驗證,如下所示。
這樣提交成功后,會同時在服務器的云解析上面自動增加一條記錄,如下所示
如果我們的申請的免費SSL證書獲得通過,那么狀態(tài)會變?yōu)椤疽押灠l(fā)】,如下所示,這個時候就可以用了。
?下載的證書包括幾個文件,如下所示。
我們在IIS服務器上雙擊pfx文件,默認一步步操作即可把證書增加加到對應的目錄里面了。
接著我們可以在控制臺中查看到對應的證書位置。
然后在IIS里面綁定443端口,選擇對應的SSL證書即可完成對SSL證書的綁定了,如下圖所示。
這個時候,如果我們訪問網(wǎng)站(我們官網(wǎng)是https://www.iqidi.com),那么?就可以在瀏覽器的左側(cè)看到證書的信息了。
?
2、微信小程序整合處理
為了整合遠程HTTPS連接獲取數(shù)據(jù),我們需要進行部署一個Web?API的接口,那么我們可以建立一個進行MVC控制器進行測試,如下我們在控制器里面添加一個方法來獲取第三方接口的數(shù)據(jù),然后返回來給我們的小程序使用。
例如,我們以連接地址:http://m.maoyan.com/movie/list.json返回的數(shù)據(jù)為例,這個接口用來獲取電影的數(shù)據(jù),獲得的結(jié)果如下所示。
由于小程序?qū)τ蛎南拗?#xff0c;我們不能使用第三方的API接口,因此需要在自己域名內(nèi)部的API進行封裝處理,然后再提供給自己的小程序使用,我們建立一個MVC的控制器方法,如下代碼所示。
/// <summary>/// 增加一個域名內(nèi)的接口,供小程序測試/// </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測試的時候,確信能夠獲得返回的JSON數(shù)據(jù),在進行小程序的測試即可。
執(zhí)行POST數(shù)據(jù)的處理,可以獲得對應的JSON數(shù)據(jù),如下所示。
不過如果我們這個時候整合小程序進行測試,如下代碼所示。
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)站上找到對應的解決方案,測試后正確解決問題:在?PowerShell中運行以下內(nèi)容, 然后重啟服務器
# 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"這樣啟動接口,獲得數(shù)據(jù)并在小程序中進行正確展示了。
?
更多專業(yè)前端知識,請上 【猿2048】www.mk2048.com
總結(jié)
以上是生活随笔為你收集整理的开发微信小程序中SSL协议的申请、证书绑定、TLS 版本处理等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css布局笔记(二)Flex
- 下一篇: MapXtreme2008中操作矢量符号