golang包快速生成base64验证码的方法
程序员文章站
2022-06-23 18:25:00
base64captcha快速生成base64编码图片验证码字符串支持多种样式,算术,数字,字母,混合模式,语音模式.base64是网络上最常见的用于传输8bit字节代码的编码方式之一。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包
go get -u github.com/mojocn/base64captcha
创建图像验证码
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") }
验证图像验证码
import "github.com/mojocn/base64captcha" func verfiycaptcha(idkey,verifyvalue string){ verifyresult := base64captcha.verifycaptcha(idkey, verifyvalue) if verifyresult { //success } else { //fail } }
使用golang搭建api服务
// 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代码
cd $gopath/src/github.com/mojocn/captcha/examples go run main.go
访问
到此这篇关于golang包快速生成base64验证码的方法的文章就介绍到这了,更多相关golang base64验证码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!