- 1. 📒 文档&Demo 📒
- 2. 🚀 快速上手 🚀
- 2.1 📥 下载base64Captcha包 📥
- 2.2 🏂 在您的项目中使用base64Captcha 🏂
- Store interface 或者使用自带memory store 🏇">2.2.1 🏇 实现Store interface 或者使用自带memory store 🏇
- Driver interface 或者使用自带 drivers 🏄">2.2.2 🏄 实现Driver interface 或者使用自带 drivers 🏄
- captcha.go 🚴">2.2.3 🚴 核心代码captcha.go 🚴
- 2.2.4 🚵 生成Base64(image/audio)验证码字符串 🚵
- 2.2.5 🤸 校验验证码内容 🤸
- 2.2.6 🏃 完整实例代码 🏃
- 快速测试代码:
- 3. 🎨 定制自己的图形验证码 🎨
🎃Base64captcha🎃 几行代码就可以定义自己内容的图形验证码库,支持任意unicode字符的内容.
1. 📒 文档&Demo 📒
2. 🚀 快速上手 🚀
2.1 📥 下载base64Captcha包 📥
go get -u github.com/mojocn/base64Captcha
2.2 🏂 在您的项目中使用base64Captcha 🏂
2.2.1 🏇 实现Store interface 或者使用自带memory store 🏇
- Build-in Memory Store(只支持单机部署,多台服务器请自定义redis store)
type Store interface {// Set sets the digits for the captcha id.Set(id string, value string)// Get returns stored digits for the captcha id. Clear indicates// whether the captcha must be deleted from the store.Get(id string, clear bool) string//Verify captcha's answer directlyVerify(id, answer string, clear bool) bool}
2.2.2 🏄 实现Driver interface 或者使用自带 drivers 🏄
包自带driver:
// Driver captcha interface for captcha engine to to write stafftype Driver interface {//DrawCaptcha draws binary itemDrawCaptcha(content string) (item Item, err error)//GenerateIdQuestionAnswer creates rand id, content and answerGenerateIdQuestionAnswer() (id, q, a string)}
2.2.3 🚴 核心代码captcha.go 🚴
captcha.go 是package的入口文件,源代码逻辑非常简单,如下:
func init() {//init rand seedrand.Seed(time.Now().UnixNano())}// Captcha captcha basic information.type Captcha struct {Driver DriverStore Store}func NewCaptcha(driver Driver, store Store) *Captcha {return &Captcha{Driver: driver, Store: store}}//Generate generates a random id, base64 image string or an error if anyfunc (c *Captcha) Generate() (id, b64s string, err error) {id,content, answer := c.Driver.GenerateIdQuestionAnswer()item, err := c.Driver.DrawCaptcha(content)if err != nil {return "", "", err}c.Store.Set(id, answer)b64s = item.EncodeB64string()return}//if you has multiple captcha instances which shares a same store. You may want to use `store.Verify` method instead.//Verify by given id key and remove the captcha value in store, return boolean value.func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {match = c.Store.Get(id, clear) == answerreturn}
2.2.4 🚵 生成Base64(image/audio)验证码字符串 🚵
//Generate generates a random id, base64 image string or an error if anyfunc (c *Captcha) Generate() (id, b64s string, err error) {id,content, answer := c.Driver.GenerateIdQuestionAnswer()item, err := c.Driver.DrawCaptcha(content)if err != nil {return "", "", err}c.Store.Set(id, answer)b64s = item.EncodeB64string()return}
2.2.5 🤸 校验验证码内容 🤸
//if you has multiple captcha instances which shares a same store. You may want to use `store.Verify` method instead.//Verify by given id key and remove the captcha value in store, return boolean value.func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {match = c.Store.Get(id, clear) == answerreturn}
2.2.6 🏃 完整实例代码 🏃
// example of HTTP server that uses the captcha package.package mainimport ("encoding/json""fmt""github.com/mojocn/base64Captcha""log""net/http")//configJsonBody json request body.type configJsonBody struct {Id stringCaptchaType stringVerifyValue stringDriverAudio *base64Captcha.DriverAudioDriverString *base64Captcha.DriverStringDriverChinese *base64Captcha.DriverChineseDriverMath *base64Captcha.DriverMathDriverDigit *base64Captcha.DriverDigit}var store = base64Captcha.DefaultMemStore// base64Captcha create http handlerfunc generateCaptchaHandler(w http.ResponseWriter, r *http.Request) {//parse request parametersdecoder := json.NewDecoder(r.Body)var param configJsonBodyerr := decoder.Decode(¶m)if err != nil {log.Println(err)}defer r.Body.Close()var driver base64Captcha.Driver//create base64 encoding captchaswitch param.CaptchaType {case "audio":driver = param.DriverAudiocase "string":driver = param.DriverString.ConvertFonts()case "math":driver = param.DriverMath.ConvertFonts()case "chinese":driver = param.DriverChinese.ConvertFonts()default:driver = param.DriverDigit}c := base64Captcha.NewCaptcha(driver, store)id, b64s, err := c.Generate()body := map[string]interface{}{"code": 1, "data": b64s, "captchaId": id, "msg": "success"}if err != nil {body = map[string]interface{}{"code": 0, "msg": err.Error()}}w.Header().Set("Content-Type", "application/json; charset=utf-8")json.NewEncoder(w).Encode(body)}// base64Captcha verify http handlerfunc captchaVerifyHandle(w http.ResponseWriter, r *http.Request) {//parse request json bodydecoder := json.NewDecoder(r.Body)var param configJsonBodyerr := decoder.Decode(¶m)if err != nil {log.Println(err)}defer r.Body.Close()//verify the captchabody := map[string]interface{}{"code": 0, "msg": "failed"}if store.Verify(param.Id, param.VerifyValue, true) {body = map[string]interface{}{"code": 1, "msg": "ok"}}//set json responsew.Header().Set("Content-Type", "application/json; charset=utf-8")json.NewEncoder(w).Encode(body)}//start a net/http serverfunc main() {//serve Vuejs+ElementUI+Axios Web Applicationhttp.Handle("/", http.FileServer(http.Dir("./static")))//api for create captchahttp.HandleFunc("/api/getCaptcha", generateCaptchaHandler)//api for verify captchahttp.HandleFunc("/api/verifyCaptcha", captchaVerifyHandle)fmt.Println("Server is at :8777")if err := http.ListenAndServe(":8777", nil); err != nil {log.Fatal(err)}}
快速测试代码:
package mainimport ("log""testing""github.com/mojocn/base64Captcha")//configJsonBody json request body.type configJsonBody struct {Id stringCaptchaType stringVerifyValue stringDriverAudio *base64Captcha.DriverAudioDriverString *base64Captcha.DriverStringDriverChinese *base64Captcha.DriverChineseDriverMath *base64Captcha.DriverMathDriverDigit *base64Captcha.DriverDigit}var store = base64Captcha.DefaultMemStore// base64Captcha create return id, b64s, errfunc GetCaptcha() (string, string, error) {// {// ShowLineOptions: [],// CaptchaType: "string",// Id: '',// VerifyValue: '',// DriverAudio: {// Length: 6,// Language: 'zh'// },// DriverString: {// Height: 60,// Width: 240,// ShowLineOptions: 0,// NoiseCount: 0,// Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",// Length: 6,// Fonts: ["wqy-microhei.ttc"],// BgColor: {R: 0, G: 0, B: 0, A: 0},// },// DriverMath: {// Height: 60,// Width: 240,// ShowLineOptions: 0,// NoiseCount: 0,// Length: 6,// Fonts: ["wqy-microhei.ttc"],// BgColor: {R: 0, G: 0, B: 0, A: 0},// },// DriverChinese: {// Height: 60,// Width: 320,// ShowLineOptions: 0,// NoiseCount: 0,// Source: "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,,不想要,的值",// Length: 2,// Fonts: ["wqy-microhei.ttc"],// BgColor: {R: 125, G: 125, B: 0, A: 118},// },// DriverDigit: {// Height: 80,// Width: 240,// Length: 5,// MaxSkew: 0.7,// DotCount: 80// }// },// blob: "",// loading: false// }// https://captcha.mojotv.cn/ 调试配置var param configJsonBody = configJsonBody{Id: "",CaptchaType: "string",VerifyValue: "",DriverAudio: &base64Captcha.DriverAudio{},DriverString: &base64Captcha.DriverString{Length: 4,Height: 60,Width: 240,ShowLineOptions: 2,NoiseCount: 0,Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",},DriverChinese: &base64Captcha.DriverChinese{},DriverMath: &base64Captcha.DriverMath{},DriverDigit: &base64Captcha.DriverDigit{},}var driver base64Captcha.Driver//create base64 encoding captchaswitch param.CaptchaType {case "audio":driver = param.DriverAudiocase "string":driver = param.DriverString.ConvertFonts()case "math":driver = param.DriverMath.ConvertFonts()case "chinese":driver = param.DriverChinese.ConvertFonts()default:driver = param.DriverDigit}c := base64Captcha.NewCaptcha(driver, store)return c.Generate()// id, b64s, err := c.Generate()// body := map[string]interface{}{"code": 1, "data": b64s, "captchaId": id, "msg": "success"}// if err != nil {// body = map[string]interface{}{"code": 0, "msg": err.Error()}// }// var _ = body// // log.Println(body)// log.Println(1)// log.Println(id)// log.Printf("store =%+v\n", store)}// base64Captcha verifyfunc VerifyCaptcha(id, VerifyValue string) bool {return store.Verify(id, VerifyValue, true)}// go test -test.run=TestMyCaptcha -count=1 -v ./func TestMyCaptcha(t *testing.T) {id, b64s, err := GetCaptcha()if err != nil {return}var _ = b64slog.Println("id =", id)log.Println("VerifyValue =", store.Get(id, true))result := VerifyCaptcha(id, store.Get(id, true))log.Println("result =", result)}
3. 🎨 定制自己的图形验证码 🎨
您那个定制自己的图形验码内容,只需实现 interface driver 和 interface item.
下面是几个可以参考的driver实现示例:
您甚至可以设计captcha struct成您想要的功能
