一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Golang - golang包快速生成base64驗證碼的方法

golang包快速生成base64驗證碼的方法

2020-06-07 11:59林臺山人 Golang

這篇文章主要介紹了golang包快速生成base64驗證碼的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

base64Captcha快速生成base64編碼圖片驗證碼字符串

支持多種樣式,算術,數字,字母,混合模式,語音模式.

Base64是網絡上最常見的用于傳輸8Bit字節代碼的編碼方式之一。Base64編碼可用于在HTTP環境下傳遞較長的標識信息, 直接把base64當成是字符串方式的數據就好了

減少了http請求;數據就是圖片;

為APIs微服務而設計

為什么base64圖片 for RESTful 服務

Data URIs 支持大部分瀏覽器,IE8之后也支持.

小圖片使用base64響應對于RESTful服務來說更便捷
godoc文檔

在線Demo Playground Powered by Vuejs+elementUI+Axios

golang包快速生成base64驗證碼的方法

安裝golang

?
1
go get -u github.com/mojocn/base64Captcha

創建圖像驗證碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import "github.com/mojocn/base64Captcha"
func demoCodeCaptchaCreate() {
  //config struct for digits
  //數字驗證碼配置
  var configD = base64Captcha.ConfigDigit{
    Height:   80,
    Width:   240,
    MaxSkew:  0.7,
    DotCount:  80,
    CaptchaLen: 5,
  }
  //config struct for audio
  //聲音驗證碼配置
  var configA = base64Captcha.ConfigAudio{
    CaptchaLen: 6,
    Language:  "zh",
  }
  //config struct for Character
  //字符,公式,驗證碼配置
  var configC = base64Captcha.ConfigCharacter{
    Height:       60,
    Width:       240,
    //const CaptchaModeNumber:數字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算術,CaptchaModeNumberAlphabet:數字字母混合.
    Mode:        base64Captcha.CaptchaModeNumber,
    ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
    ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
    IsShowHollowLine:  false,
    IsShowNoiseDot:   false,
    IsShowNoiseText:  false,
    IsShowSlimeLine:  false,
    IsShowSineLine:   false,
    CaptchaLen:     6,
  }
  //create a audio captcha.
  idKeyA, capA := base64Captcha.GenerateCaptcha("", configA)
  //以base64編碼
  base64stringA := base64Captcha.CaptchaWriteToBase64Encoding(capA)
  //create a characters captcha.
  idKeyC, capC := base64Captcha.GenerateCaptcha("", configC)
  //以base64編碼
  base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC)
  //create a digits captcha.
  idKeyD, capD := base64Captcha.GenerateCaptcha("", configD)
  //以base64編碼
  base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
   
  fmt.Println(idKeyA, base64stringA, "\n")
  fmt.Println(idKeyC, base64stringC, "\n")
  fmt.Println(idKeyD, base64stringD, "\n")
}

驗證圖像驗證碼

?
1
2
3
4
5
6
7
8
9
import "github.com/mojocn/base64Captcha"
func verfiyCaptcha(idkey,verifyValue string){
  verifyResult := base64Captcha.VerifyCaptcha(idkey, verifyValue)
  if verifyResult {
    //success
  } else {
    //fail
  }
}

使用golang搭建API服務

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// example of HTTP server that uses the captcha package.
package main
 
import (
  "encoding/json"
  "fmt"
  "github.com/mojocn/base64Captcha"
  "log"
  "net/http"
)
 
//ConfigJsonBody json request body.
type ConfigJsonBody struct {
  Id       string
  CaptchaType   string
  VerifyValue   string
  ConfigAudio   base64Captcha.ConfigAudio
  ConfigCharacter base64Captcha.ConfigCharacter
  ConfigDigit   base64Captcha.ConfigDigit
}
 
var configC = base64Captcha.ConfigCharacter{
  Height:       60,
  Width:       240,
  Mode:        0,
  ComplexOfNoiseText: 0,
  ComplexOfNoiseDot: 0,
  IsShowHollowLine:  false,
  IsShowNoiseDot:   false,
  IsShowNoiseText:  false,
  IsShowSlimeLine:  false,
  IsShowSineLine:   false,
  CaptchaLen:     6,
}
 
 
// base64Captcha create http handler
func generateCaptchaHandler(w http.ResponseWriter, r *http.Request) {
  //parse request parameters
  //接收客戶端發送來的請求參數
  decoder := json.NewDecoder(r.Body)
  var postParameters ConfigJsonBody
  err := decoder.Decode(&postParameters)
  if err != nil {
    log.Println(err)
  }
  defer r.Body.Close()
 
  //create base64 encoding captcha
  //創建base64圖像驗證碼
 
  var config interface{}
  switch postParameters.CaptchaType {
  case "audio":
    config = postParameters.ConfigAudio
  case "character":
    config = postParameters.ConfigCharacter
  default:
    config = postParameters.ConfigDigit
  }
  captchaId, digitCap := base64Captcha.GenerateCaptcha(postParameters.Id, config)
  base64Png := base64Captcha.CaptchaWriteToBase64Encoding(digitCap)
 
  //or you can do this
  //你也可以是用默認參數 生成圖像驗證碼
  //base64Png := captcha.GenerateCaptchaPngBase64StringDefault(captchaId)
 
  //set json response
  //設置json響應
 
  w.Header().Set("Content-Type", "application/json; charset=utf-8")
  body := map[string]interface{}{"code": 1, "data": base64Png, "captchaId": captchaId, "msg": "success"}
  json.NewEncoder(w).Encode(body)
}
// base64Captcha verify http handler
func captchaVerifyHandle(w http.ResponseWriter, r *http.Request) {
 
  //parse request parameters
  //接收客戶端發送來的請求參數
  decoder := json.NewDecoder(r.Body)
  var postParameters ConfigJsonBody
  err := decoder.Decode(&postParameters)
  if err != nil {
    log.Println(err)
  }
  defer r.Body.Close()
  //verify the captcha
  //比較圖像驗證碼
  verifyResult := base64Captcha.VerifyCaptcha(postParameters.Id, postParameters.VerifyValue)
 
  //set json response
  //設置json響應
  w.Header().Set("Content-Type", "application/json; charset=utf-8")
  body := map[string]interface{}{"code": "error", "data": "驗證失敗", "msg": "captcha failed"}
  if verifyResult {
    body = map[string]interface{}{"code": "success", "data": "驗證通過", "msg": "captcha verified"}
  }
  json.NewEncoder(w).Encode(body)
}
 
//start a net/http server
//啟動golang net/http 服務器
func main() {
 
  //serve Vuejs+ElementUI+Axios Web Application
  http.Handle("/", http.FileServer(http.Dir("./static")))
 
  //api for create captcha
  http.HandleFunc("/api/getCaptcha", generateCaptchaHandler)
 
  //api for verify captcha
  http.HandleFunc("/api/verifyCaptcha", captchaVerifyHandle)
 
  fmt.Println("Server is at localhost:3333")
  if err := http.ListenAndServe("localhost:3333", nil); err != nil {
    log.Fatal(err)
  }
}

運行demo代碼

?
1
2
cd $GOPATH/src/github.com/mojocn/captcha/examples
go run main.go

訪問 http://localhost:777

 到此這篇關于golang包快速生成base64驗證碼的方法的文章就介紹到這了,更多相關golang base64驗證碼內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/dfsxh/p/10243542.html

延伸 · 閱讀

精彩推薦
  • GolangGolang通脈之數據類型詳情

    Golang通脈之數據類型詳情

    這篇文章主要介紹了Golang通脈之數據類型,在編程語言中標識符就是定義的具有某種意義的詞,比如變量名、常量名、函數名等等,Go語言中標識符允許由...

    4272021-11-24
  • Golanggolang的httpserver優雅重啟方法詳解

    golang的httpserver優雅重啟方法詳解

    這篇文章主要給大家介紹了關于golang的httpserver優雅重啟的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,...

    helight2992020-05-14
  • Golanggolang json.Marshal 特殊html字符被轉義的解決方法

    golang json.Marshal 特殊html字符被轉義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉義的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 ...

    李浩的life12792020-05-27
  • Golanggo日志系統logrus顯示文件和行號的操作

    go日志系統logrus顯示文件和行號的操作

    這篇文章主要介紹了go日志系統logrus顯示文件和行號的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    SmallQinYan12302021-02-02
  • Golanggo語言制作端口掃描器

    go語言制作端口掃描器

    本文給大家分享的是使用go語言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
  • Golanggolang如何使用struct的tag屬性的詳細介紹

    golang如何使用struct的tag屬性的詳細介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細介紹,從例子說起,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看...

    Go語言中文網11352020-05-21
  • GolangGolang中Bit數組的實現方式

    Golang中Bit數組的實現方式

    這篇文章主要介紹了Golang中Bit數組的實現方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    天易獨尊11682021-06-09
  • Golanggolang 通過ssh代理連接mysql的操作

    golang 通過ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過ssh代理連接mysql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...

    a165861639710342021-03-08
主站蜘蛛池模板: 无毛黄片 | 女生被草 | 亚洲 欧美 日韩 综合 | 91久久夜色精品国产九色 | 操弄哥哥的108种姿势 | 国产免费一区二区三区 | 女女同性做爰xxoo亲吻 | 亚裔aⅴ艳星katsuni | 逼里逼里香 | 99影视在线视频免费观看 | 午夜dj影院在线观看完整版 | 亚洲天堂视频在线免费观看 | 亚洲欧美在线观看首页 | 五月天精品视频播放在线观看 | 麻豆最新地址 | 极品 女神校花 露脸91 | 91精品免费国产高清在线 | 色香婷婷| 2021国产精品成人免费视频 | 扒开双腿羞辱调教play视频 | 亚洲免费在线看 | 久久久久青草大香线综合精品 | 国产精品视频1区 | 国模娜娜一区二区三区 | 欧美日韩高清观看一区二区 | 午色影院| 精品国产一区二区三区久 | 免费aⅴ在线 | 成人精品一区久久久久 | 美女机机对机机的视频(免费) | 失禁尿丝袜vk | 公妇乱淫在线播放免费观看 | 30分钟的高清视频在线观看 | 亚洲日韩精品欧美一区二区 | 青青操在线 | 猫咪maomiav永久网址 | 国产a在线 | 特级毛片全部免费播放器 | 四虎黄色影视库 | 激情图片 激情小说 | 色狠狠婷婷97 |