<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go 項目實戰(zhàn):Golang HTTP 驗證碼

          共 3009字,需瀏覽 7分鐘

           ·

          2020-08-22 03:09

          點擊上方藍(lán)色“Go語言中文網(wǎng)”關(guān)注我們,領(lǐng)全套Go資料,每天學(xué)習(xí)?Go?語言

          驗證碼(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自動區(qū)分計算機(jī)[1]和人類的圖靈測試[2])的縮寫,是一種區(qū)分用戶是計算機(jī)還是人的公共全自動程序[3]。可以防止:惡意破解密碼、刷票[4]、論壇灌水,有效防止某個黑客對某一個特定注冊用戶用特定程序暴力破解方式進(jìn)行不斷的登陸嘗試,實際上用驗證碼是現(xiàn)在很多網(wǎng)站通行的方式,我們利用比較簡易的方式實現(xiàn)了這個功能。這個問題可以由計算機(jī)生成并評判,但是必須只有人類才能解答。由于計算機(jī)無法解答CAPTCHA的問題,所以回答出問題的用戶就可以被認(rèn)為是人類。

          傳統(tǒng)網(wǎng)站驗證碼工作機(jī)制

          1. 客戶端請求服務(wù)器獲取驗證碼圖片
          2. 服務(wù)器生成隨機(jī)串(驗證碼值)寫入Session,并將驗證碼值寫入到圖片中返回給客戶端
          3. 客戶端輸入圖片上的字符串提交給服務(wù)器驗證
          4. 服務(wù)器比對客戶端提交的字符串值和 Session 中是否匹配,如果匹配則通過驗證

          由于服務(wù)器生成的驗證碼值從始至終均未返回給客戶端,因此,客戶端只能從圖片中識別驗證碼字符串,從而保證人機(jī)校驗邏輯。

          Go的HTTP驗證碼

          思路

          Go 語言的 HTTP 服務(wù)器默認(rèn)不支持 Session,因此驗證碼值需要換個思路存儲,以下是不使用 Session 的邏輯

          1. 客戶端請求服務(wù)器獲取驗證碼ID
          2. 服務(wù)器生成驗證碼 ID,并生成驗證碼值,將 ID 和值的映射關(guān)系記錄到內(nèi)存或緩存,并將 ID 返回給客戶端
          3. 客戶端根據(jù)返回的 ID 請求服務(wù)器獲取驗證碼圖片
          4. 服務(wù)器獲取到驗證碼 ID,從內(nèi)存或緩存中取出驗證碼值,將該值寫入圖片并將圖片返回給客戶端
          5. 客戶端提交驗證碼 ID(第1步獲得)和驗證碼值給服務(wù)器驗證
          6. 服務(wù)器獲取驗證碼 ID,從內(nèi)存或緩存中取出驗證碼值與客戶端提交的驗證碼值比對

          示例

          1. 安裝驗證碼依賴
          go?get?-u?github.com/dchest/captcha
          1. 代碼實現(xiàn)
          package?main
          ???
          ???import?(
          ???????"fmt"
          ???????"github.com/dchest/captcha"
          ???????"log"
          ???????"net/http"
          ???)
          ???
          ???func?main()?{
          ???????//?獲取驗證碼?ID
          ???????http.HandleFunc("/captcha/generate",?func(w?http.ResponseWriter,?r?*http.Request)?{
          ???????????id?:=?captcha.NewLen(6)
          ???????????if?_,?err?:=?fmt.Fprint(w,?id);?err?!=?nil?{
          ???????????????log.Println("generate?captcha?error",?err)
          ???????????}
          ???????})
          ???????//?獲取驗證碼圖片
          ???????http.HandleFunc("/captcha/image",?func(w?http.ResponseWriter,?r?*http.Request)?{
          ???????????id?:=?r.URL.Query().Get("id")
          ???????????if?id?==?""?{
          ???????????????http.Error(w,?"Bad?Request",?http.StatusBadRequest)
          ???????????????return
          ???????????}
          ???????????w.Header().Set("Content-Type",?"image/png")
          ???????????if?err?:=?captcha.WriteImage(w,?id,?120,?80);?err?!=?nil?{
          ???????????????log.Println("show?captcha?error",?err)
          ???????????}
          ???????})
          ???????//?業(yè)務(wù)處理
          ???????http.HandleFunc("/login",?func(w?http.ResponseWriter,?r?*http.Request)?{
          ???????????if?err?:=?r.ParseForm();?err?!=?nil?{
          ???????????????log.Println("parseForm?error",?err)
          ???????????????http.Error(w,?"Internal?Error",?http.StatusInternalServerError)
          ???????????????return
          ???????????}
          ???????????//?獲取驗證碼?ID?和驗證碼值
          ???????????id?:=?r.FormValue("id")
          ???????????value?:=?r.FormValue("value")
          ???????????//?比對提交的驗證碼值和內(nèi)存中的驗證碼值
          ???????????if?captcha.VerifyString(id,?value)?{
          ???????????????fmt.Fprint(w,?"ok")
          ???????????}?else?{
          ???????????????fmt.Fprint(w,?"mismatch")
          ???????????}
          ???????})
          ???????log.Fatal(http.ListenAndServe(":8080",?nil))
          ???}
          1. 運行
            1. 訪問/captcha/generate獲得驗證碼 ID
            2. 訪問/captcha/image?id=驗證碼 ID
            3. 訪問/login,并輸入第一步的驗證碼 ID 和第二步的驗證碼值即可查看驗證結(jié)果

          項目地址

          完整代碼 https://github.com/xialeistudio/go-http-captcha-example。

          本文作者:xialeistudio

          原文鏈接:https://segmentfault.com/a/1190000023703468

          參考資料

          [1]

          計算機(jī): https://baike.baidu.com/item/計算機(jī)

          [2]

          圖靈測試: https://baike.baidu.com/item/圖靈測試

          [3]

          程序: https://baike.baidu.com/item/程序/71525

          [4]

          刷票: https://baike.baidu.com/item/刷票/6540942



          推薦閱讀


          學(xué)習(xí)交流 Go 語言,掃碼回復(fù)「進(jìn)群」即可


          站長 polarisxu

          自己的原創(chuàng)文章

          不限于 Go 技術(shù)

          職場和創(chuàng)業(yè)經(jīng)驗


          Go語言中文網(wǎng)

          每天為你

          分享 Go 知識

          Go愛好者值得關(guān)注




          瀏覽 52
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩无码高清视频 | 精品毛片一区二区免费看 | 老鸭窝在线观看视频 | 黄色视频日本免费 | 超碰对白 |