头图

介绍一个 golang 图像验证码,简单易用,高安全性的“行为式验证码”Go库

go-captcha

go-captcha, 一个简洁易用、交互友好、高安全性的点选行为验证码 Go 库 ,采用 “验证码展示-采集用户行为-验证行为数据” 为流程,用户无需键盘手动输入,极大优化传统验证码用户体验不佳的问题,支持PC端及移动端,带有与前端交互的DEMO。

http://47.104.180.148/go-captcha/go-captcha-01.png

http://47.104.180.148/go-captcha/go-captcha.jpg

中国Go模块代理

设置Go模块的代理
  • Window

    $ set GO111MODULE=on
    $ set GOPROXY=https://goproxy.io,direct
    
    ### The Golang 1.13+ can be executed directly
    $ go env -w GO111MODULE=on
    $ go env -w GOPROXY=https://goproxy.io,direct
  • Linux or Mac

    $ export GO111MODULE=on
    $ export GOPROXY=https://goproxy.io,direct
    
    ### or
    $ echo "export GO111MODULE=on" >> ~/.profile
    $ echo "export GOPROXY=https://goproxy.cn,direct" >> ~/.profile
    $ source ~/.profile

依赖golang官方标准库

$ go get -u github.com/golang/freetype
$ go get -u golang.org/x/crypto
$ go get -u golang.org/x/image

安装模块

$ go get -u github.com/wenlng/go-captcha/captcha

引入模块

package main

import "github.com/wenlng/go-captcha/captcha"

func main(){
   // ....
}

快速使用

package main
import (
    "fmt"
    "os"
    "github.com/wenlng/go-captcha/captcha"
)

func main(){
    // Captcha Single Instances
    capt := captcha.GetCaptcha()
    
    // 生成验证码
    dots, b64, tb64, key, err := capt.Generate()
    if err != nil {
        panic(err)
        return
    }
    
    // 主图base64
    fmt.Println(len(b64))
    
    // 缩略图base64
    fmt.Println(len(tb64))
    
    // 唯一key
    fmt.Println(key)
    
    // 文本位置验证数据
    fmt.Println(dots)
}

验证码实例

  • 创建实例或者获取单例模式的实例

    package main
    import (
      "fmt"
      "github.com/wenlng/go-captcha/captcha"
    )
    
    func main(){
      // 创建验证码实例
      // capt := captcha.NewCaptcha() 
      
      // 单例模式的验证码实例
      capt := captcha.GetCaptcha()
    
      // ====================================================
      fmt.Println(capt)
    
    }

验证码配置

你可以直接拷贝实例中 "__example/resources" 的图片和字体文件到你的项目中使用

文本相关的配置
package main
import (
    "fmt"
    "github.com/wenlng/go-captcha/captcha"
)

func main(){
    capt := captcha.GetCaptcha()
    
    // ====================================================
    // Method: SetRangChars (chars []string) error;
    // Desc: Set random char of captcha
    // ====================================================
    // Letter
    //chars := "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    //_ = capt.SetRangChars(strings.Split(chars, ""))
    
    // Two Letter
    //chars := []string{"HE","CA","WO","NE","HT","IE","PG","GI","CH","CO","DA"}
    //_ = capt.SetRangChars(chars)

    // Chinese Char
    chars := []string{"你","好","呀","这","是","点","击","验","证","码","哟"}
    _ = capt.SetRangChars(chars)

    // ====================================================
    fmt.Println(capt)
}
图片相关的配置
package main
import (
    "fmt"
    "os"
    "github.com/wenlng/go-captcha/captcha"
)

func main(){
    capt := captcha.GetCaptcha()
    
    path, _ := os.Getwd()    
    // ====================================================
    // Method: SetBackground(color []string);
    // Desc: 设置验证码背景图
    // ====================================================
    capt.SetBackground([]string{
        path + "/__example/resources/images/1.jpg",
        path + "/__example/resources/images/2.jpg",
    })

    // ====================================================
    // Method: SetFont(fonts []string);
    // Desc: 设置验证码字体
    // ====================================================
    capt.SetFont([]string{
        path + "/__example/resources/fonts/fzshengsksjw_cu.ttf",
        path + "/__example/resources/fonts/fzssksxl.ttf",
    })

    // ====================================================
    // Method: SetImageSize(size *Size);
    // Desc: 设置验证码主图的尺寸
    // ====================================================
    capt.SetImageSize(&captcha.Size{300, 300})

    // ====================================================
    // Method: SetThumbSize(size *Size);
    // Desc: 设置验证码缩略图的尺寸
    // ====================================================
    capt.SetThumbSize(&captcha.Size{150, 40})

    // ====================================================
    // Method: SetFontDPI(val int);
    // Desc: 设置验证码字体的随机DPI,最好是72
    // ====================================================
    capt.SetFontDPI(72)

    // ====================================================
    // Method: SetTextRangLen(val *captcha.RangeVal);
    // Desc: 设置验证码文本总和的随机长度范围
    // ====================================================
    capt.SetTextRangLen(&captcha.RangeVal{6, 7})

    // ====================================================
    // Method: SetRangFontSize(val *captcha.RangeVal);
    // Desc: 设置验证码文本的随机大小
    // ====================================================
    capt.SetRangFontSize(&captcha.RangeVal{32, 42})

    // ====================================================
    // Method: SetRangCheckTextLen(val *captcha.RangeVal);
    // Desc:设置验证码校验文本的随机长度的范围
    // ====================================================
    capt.SetRangCheckTextLen(&captcha.RangeVal{2, 4})

    // ====================================================
    // Method: SetRangCheckFontSize(val *captcha.RangeVal);
    // Desc:设置验证码文本的随机大小
    // ====================================================
    capt.SetRangCheckFontSize(&captcha.RangeVal{24, 30})
    
    // ====================================================
    // Method: SetTextRangFontColors(colors []string);
    // Desc: 设置验证码文本的随机十六进制颜色
    // ====================================================
    capt.SetTextRangFontColors([]string{
        "#1d3f84",
        "#3a6a1e",
    })
 
    // ====================================================
    // Method: SetThumbTextRangFontColors(colors []string);
    // Desc: Set random hex color of captcha text
    // ====================================================
    capt.SetThumbTextRangFontColors([]string{
        "#1d3f84",
        "#3a6a1e",
    })

    // ====================================================
    // Method: SetImageFontAlpha(val float64);
    // Desc:设置验证码字体的透明度
    // ====================================================
    capt.SetImageFontAlpha(0.5)

    // ====================================================
    // Method: SetTextRangAnglePos(pos []*RangeVal);
    // Desc:设置验证码文本的角度
    // ====================================================
    capt.SetTextRangAnglePos([]*captcha.RangeVal{
        {1, 15},
        {15, 30},
        {30, 45},
        {315, 330},
        {330, 345},
        {345, 359},
    })

    // ====================================================
    // Method: SetImageFontDistort(val int);
    // Desc:设置验证码字体扭曲程度
    // ====================================================
    capt.SetImageFontDistort(captcha.DistortLevel2)
  
    // ====================================================
    // Method: SetThumbBgColors(colors []string);
    // Desc: 设置缩略验证码背景的随机十六进制颜色
    // ====================================================
    capt.SetThumbBgColors([]string{
        "#1d3f84",
        "#3a6a1e",
    })

    // ====================================================
    // Method: SetThumbBackground(colors []string);
    // Desc:设置缩略验证码随机图像背景
    // ====================================================
    capt.SetThumbBackground([]string{
        path + "/__example/resources/images/r1.jpg",
        path + "/__example/resources/images/r2.jpg",
    })

    // ====================================================
    // Method: SetThumbBgDistort(val int);
    // Desc:设置缩略验证码背景的扭曲程度
    // ====================================================
    capt.SetThumbBgDistort(captcha.DistortLevel2)

    // ====================================================
    // Method: SetThumbFontDistort(val int);
    // Desc:设置缩略验证码字体的扭曲程度
    // ====================================================
    capt.SetThumbFontDistort(captcha.DistortLevel2)

    // ====================================================
    // Method: SetThumbBgCirclesNum(val int);
    // Desc:设置验证码背景的圈点数
    // ====================================================
    capt.SetThumbBgCirclesNum(20)

    // ====================================================
    // Method: SetThumbBgSlimLineNum(val int);
    // Desc:设置验证码背景的线条数
    // ====================================================
    capt.SetThumbBgSlimLineNum(3)
    

    // ====================================================
    fmt.Println(capt)
}

生成验证码数据

package main
import (
    "fmt"
    "os"
    "github.com/wenlng/go-captcha/captcha"
)

func main(){
    capt := captcha.GetCaptcha()
        
    // generate ...
    // ====================================================
    // dots:  文本字符的位置信息
    //  - {"0":{"Index":0,"Dx":198,"Dy":77,"Size":41,"Width":54,"Height":41,"Text":"SH","Angle":6,"Color":"#885500"} ...}
    // imageBase64:  Verify the clicked image
    // thumbImageBase64: Thumb displayed
    // key: Only Key
    // ====================================================
    dots, imageBase64, thumbImageBase64, key, err := capt.Generate()
    if err != nil {
        panic(err)
        return
    }
    fmt.Println(len(imageBase64))
    fmt.Println(len(thumbImageBase64))
    fmt.Println(key)
    fmt.Println(dots)
}

在 __example 中的前端在数据请求或提交验证数据时的格式:

// Example: 获取验证码数据
API = http://....../captcha/captcha-data
    Respose Data = {
        "code": 0,
        "image_base64": "...",
        "thumb_base64": "...",
        "captcha_key": "...",
    }     

// Example: 提交校验数据 
API = http://....../captcha/check-data
    Request Data = {
        dots: "x1,y1,x2,y2,...."
        key: "......"
    }

<br/>

1 声望
0 粉丝
0 条评论
推荐阅读
go图像验证码、行为式验证码
go-captcha, 一个简洁易用、交互友好、高安全性的点选行为验证码 Go 库 ,采用 “验证码展示-采集用户行为-验证行为数据” 为流程,用户无需键盘手动输入,极大优化传统验证码用户体验不佳的问题,支持PC端及移动端。

wengaolng阅读 1.2k

「刷起来」Go必看的进阶面试题详解
逃逸分析是Go语言中的一项重要优化技术,可以帮助程序减少内存分配和垃圾回收的开销,从而提高程序的性能。下面是一道涉及逃逸分析的面试题及其详解。

王中阳Go4阅读 1.9k评论 1

封面图
初学后端,如何做好表结构设计?
这篇文章介绍了设计数据库表结构应该考虑的4个方面,还有优雅设计的6个原则,举了一个例子分享了我的设计思路,为了提高性能我们也要从多方面考虑缓存问题。

王中阳Go4阅读 1.7k评论 2

封面图
又一款眼前一亮的Linux终端工具!
今天给大家介绍一款最近发现的功能十分强大,颜值非常高的一款终端工具。这个神器我是在其他公众号文章上看到的,但他们都没把它的强大之处介绍明白,所以我自己体验一波后,再向大家分享自己的体验。

良许5阅读 1.8k

一分钟搞明白!快速掌握 Go WebAssembly
最近因为各种奇怪的原因,更多的接触到了 WebAssembly。虽然之前很多博客也翻过写过各种文章,但总感觉欠些味道。于是今天梳理了一版,和大家一起展开学习。

煎鱼4阅读 2.2k

面试官:请说一下如何优化结构体的性能?
使用内存对齐机制优化结构体性能,妙啊!前言之前分享过2篇结构体文章:10秒改struct性能直接提升15%,产品姐姐都夸我好棒 和 Go语言空结构体这3种妙用,你知道吗? 得到了大家的好评。这篇继续分享进阶内容:结...

王中阳Go4阅读 3.8k评论 2

封面图
go 协程操作map导致的数据竞争及解决方法
有个查询结果集的操作,无可避免的需要在循环获取数据,然后将结果集放到 map 中,这个操作在压测的时候,没出现问题,发布到生产环境之后,开始偶现 fatal error: concurrent map read and map write 错误,导致...

hxd_5阅读 839评论 4

1 声望
0 粉丝
宣传栏