go验证码的使用
前言:
go验证码演示地址: https://captcha.mojotv.cn/
正文:
验证码包下载
go version > 1.11
go get -u github.com/mojocn/base64Captcha
验证码展示到页面示例:
使用go net http包显示验证码
import ( "encoding/json" "github.com/mojocn/base64Captcha" "html/template" "image/color" "log" "net/http" ) var store = base64Captcha.DefaultMemStore // 获取验证码 func getCaptcha(w http.ResponseWriter, r *http.Request) { //配置验证码 driverString := base64Captcha.DriverString{ Height: 60, Width: 200, NoiseCount: 0, //噪点数 ShowLineOptions: 2 | 4, //干扰线 Length: 4, Source: "123456789ABCDEFGHIJKLHIJKLMNOPQRSTUVWXYZ", BgColor: &color.RGBA{ R: 10, G: 20, B: 50, A: 10, }, Fonts: []string{"wqy-microhei.ttc"}, } var driver base64Captcha.Driver = driverString.ConvertFonts() //生成验证码 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) } // 展示页面 func index(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("view/index.html") if err != nil { log.Fatal("err:", err.Error()) } t.Execute(w, nil) } func main() { http.HandleFunc("/index", index) http.HandleFunc("/api/getCaptcha", getCaptcha) //运行http服务 http.ListenAndServe(":8080", nil) }
view/index.html页面
<h3>验证码</h3> <p><img src="#" alt="验证码" id="img" onclick="showCaptcha()"></p> <p><input type="text" placeholder="请输入验证码" name="captcha"> <input type="button" value="提交"></p> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.js"></script> <script> function showCaptcha(){ $.get("/api/getCaptcha",function (res){ $("#img").attr("src",res.data) }) } showCaptcha() </script>
效果展示:
算术验证码和中文验证码示例:
import ( "encoding/json" "github.com/mojocn/base64Captcha" "html/template" "image/color" "log" "net/http" ) var store = base64Captcha.DefaultMemStore // 获取验证码 func getCaptcha(w http.ResponseWriter, r *http.Request) { //配置验证码 driverMath := base64Captcha.DriverMath{ Height: 60, Width: 200, ShowLineOptions: 2 | 4, //干扰线 BgColor: &color.RGBA{ R: 10, G: 20, B: 50, A: 10, }, Fonts: []string{"wqy-microhei.ttc"}, } var driver base64Captcha.Driver = driverMath.ConvertFonts() //生成验证码 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) } // 展示页面 func index(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("view/index.html") if err != nil { log.Fatal("err:", err.Error()) } t.Execute(w, nil) } func main() { http.HandleFunc("/index", index) http.HandleFunc("/api/getCaptcha", getCaptcha) //运行http服务 http.ListenAndServe(":8080", nil) }
算术验证码效果展示:
中文验证码示例:
import ( "encoding/json" "github.com/mojocn/base64Captcha" "html/template" "image/color" "log" "net/http" ) var store = base64Captcha.DefaultMemStore // 获取验证码 func getCaptcha(w http.ResponseWriter, r *http.Request) { driverChinese := base64Captcha.DriverChinese{ Height: 60, Width: 200, ShowLineOptions: 2 | 4, //干扰线 Source: "大家啊第三方阿斯顿发的而且我和公司颠覆三观啊啊的发请求而且嘎达", Length: 3, BgColor: &color.RGBA{ R: 10, G: 20, B: 50, A: 10, }, Fonts: []string{"wqy-microhei.ttc"}, } var driver base64Captcha.Driver = driverChinese.ConvertFonts() //生成验证码 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) } // 展示页面 func index(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("view/index.html") if err != nil { log.Fatal("err:", err.Error()) } t.Execute(w, nil) } func main() { http.HandleFunc("/index", index) http.HandleFunc("/api/getCaptcha", getCaptcha) //运行http服务 http.ListenAndServe(":8080", nil) }
展示效果:
验证验证码输入是否正确示例:
需要注意,验证码,不论验证成功,或失败,都要重新刷新,这样确保验证码的安全效果。
var store = base64Captcha.DefaultMemStore // 获取验证码 func getCaptcha(w http.ResponseWriter, r *http.Request) { var driver = &base64Captcha.DriverDigit{ Height: 60, Width: 200, Length: 4, DotCount: 80, } //生成验证码 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) } // 验证码验证 func verifyCaptcha(w http.ResponseWriter, r *http.Request) { captcha := r.PostFormValue("captcha") captcha_id := r.PostFormValue("captcha_id") body := map[string]interface{}{"code": 0, "msg": "failed"} if store.Verify(captcha_id, captcha, true) { body = map[string]interface{}{"code": 1, "msg": "ok"} } //set json response w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(body) } // 展示页面 func index(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("view/index.html") if err != nil { log.Fatal("err:", err.Error()) } t.Execute(w, nil) } func main() { http.HandleFunc("/index", index) http.HandleFunc("/api/getCaptcha", getCaptcha) http.HandleFunc("/api/verifyCaptcha", verifyCaptcha) //运行http服务 http.ListenAndServe(":8080", nil) }
完结
但行好事,莫问前程!
本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/17311731.html