Golang 生成防识别的图片验证码
🕞 Tue, 23 Oct 2018 by GolangNote
验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。
验证码字符
如果考虑用户输入体验,一般只用数字验证码,考虑到安全,防识别、碰撞,要生成6个数字的验证码,并且使字符弯曲变形、变色、加干扰。如下图效果:
captcha
github 上的 github.com/dchest/captcha
库已经做得很好,下面是参照官方的例子做一下改动。
package main
import (
"fmt"
"github.com/dchest/captcha"
"io"
"log"
"net/http"
"text/template"
)
var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))
func showFormHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
d := struct {
CaptchaId string
}{
captcha.New(),
}
if err := formTemplate.Execute(w, &d); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func processFormHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
} else {
io.WriteString(w, "Great job, human! You solved the captcha.\n")
}
io.WriteString(w, "<br><a href='/'>Try another one</a>")
}
func main() {
http.HandleFunc("/", showFormHandler)
http.HandleFunc("/process", processFormHandler)
http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
fmt.Println("Server is at localhost:8666")
if err := http.ListenAndServe("localhost:8666", nil); err != nil {
log.Fatal(err)
}
}
const formTemplateSrc = `<!doctype html>
<head><title>Captcha Example</title></head>
<body>
<script>
function setSrcQuery(e, q) {
var src = e.src;
var p = src.indexOf('?');
if (p >= 0) {
src = src.substr(0, p);
}
e.src = src + "?" + q
}
function reload() {
setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
return false;
}
</script>
<form action="/process" method=post>
<p>Type the numbers you see in the picture below:</p>
<p><img id=image src="/captcha/{{.CaptchaId}}.png" alt="Captcha image"></p>
<a href="#" onclick="reload()">Reload</a>
<input type=hidden name=captchaId value="{{.CaptchaId}}"><br>
<input name=captchaSolution>
<input type=submit value=Submit>
</form>
`
注意事项
当验证以后 CaptchaId
会销毁,如果验证码验证不对,需要重新生成CaptchaId
并把它传给前端显示。
captcha 项目地址 https://github.com/dchest/captcha 125
本文网址: https://golangnote.com/topic/228.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- Golang实现简单的Socks5代理
- goLang 实现排列组合的代码
- 用Go语言写一个最简单的echo服务器
- golang 用正则包regexp 通过user-agent 识别手机浏览器
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang 字符串毫秒转时间格式
- Golang 简单的任务队列
- Golang telegram 机器人小试
- Golang 定时循环的实现
- golang 用 crypto/bcrypt 存储密码的例子
- Golang 生成防识别的图片验证码
- golang 实现Authenticator 二次验证,可用到web 登录
- golang 为Windows XP/Server 2003 编译程序
- Golang io.ReadCloser 和[]byte 相互转化的方法
- groupcache 使用入门
- golang 用gzip 压缩、解压缩字符串
- chroma: 纯go 实现的类似Pygments 的代码高亮库
- golang flate/zlib 解压缩
- golang 生成良好的唯一ID/uuid库比较
- Golang 生成 session id