进阶 www.iris-go.com 2026-10-20 02:39:14 · 13 阅读
第65章 反爬虫验证码(CAPTCHA)
# 第65章 反爬虫验证码(CAPTCHA)
## reCAPTCHA
reCAPTCHA 可以保护你的网站免受欺诈和滥用。它通过先进的风险分析引擎和自适应验证挑战,阻止恶意软件在你的网站上从事破坏活动。同时,正常用户仍然可以正常登录、购物、浏览页面或注册账户,而虚假用户则会被拦截。
更多信息请访问:https://www.google.com/recaptcha/about/
## 在 Iris 中使用
reCAPTCHA 是内置中间件,无需额外安装。
```go
import "github.com/kataras/iris/v12/middleware/recaptcha"
```
从 https://www.google.com/recaptcha/admin 获取你的公钥和私钥。
使用 `recaptcha.New(recaptchaSecret)` 进行初始化。
创建需要防爬虫保护的 HTML 表单,并添加 script 标签:``。
添加一个隐藏元素或 `g-recaptcha` 类的 div,并将 `data-sitekey` 属性设置为你的公钥。
使用中间件保护相关路由。
示例代码:
```go
package main
import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/recaptcha"
)
// keys should be obtained by https://www.google.com/recaptcha/admin
const (
recaptchaPublic = "6Lf3WywUAAAAAKNfAm5DP2J5ahqedtZdHTYaKkJ6"
recaptchaSecret = "6Lf3WywUAAAAAJpArb8nW_LCL_PuPuokmEABFfgw"
)
func main() {
app := iris.New()
r := recaptcha.New(recaptchaSecret)
app.Get("/comment", showRecaptchaForm)
// pass the middleware before the main handler
// or use the `recaptcha.SiteVerify`.
app.Post("/comment", r, postComment)
app.Listen(":8080")
}
// You can use view templates instead.
var htmlForm = ``
func showRecaptchaForm(ctx iris.Context) {
contents := fmt.Sprintf(htmlForm, recaptchaPublic)
ctx.HTML(contents)
}
// This handler is protected by the recaptcha middleware.
func postComment(ctx iris.Context) {
// [...]
ctx.JSON(iris.Map{"success": true})
}
```
hCaptcha Demo
```
网页服务器端代码如下:go// ./main.go
package main
import (
"os"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/hcaptcha"
)
// 在 https://dashboard.hcaptcha.com 获取以下配置值
// 同时,参考 https://docs.hcaptcha.com/#localdev 了解如何在本地环境进行测试。
var (
siteKey = os.Getenv("HCAPTCHA-SITE-KEY")
secretKey = os.Getenv("HCAPTCHA-SECRET-KEY")
)
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./templates", ".html"))
hCaptcha := hcaptcha.New(secretKey)
app.Get("/register", registerForm)
// 如需手动验证,参见 `hcaptcha.SiteVerify`。
app.Post("/register", hCaptcha, register)
app.Logger().Infof("SiteKey = %s\tSecretKey = %s",
siteKey, secretKey)
// GET: http://yourdomain.com/register
app.Listen(":80")
}
// 此处理器受 hcaptcha 中间件保护。
func register(ctx iris.Context) {
// [在此处添加相关操作...]
}
func registerForm(ctx iris.Context) {
ctx.ViewData("SiteKey", siteKey)
ctx.View("register_form.html")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
访问 http://yourdomain.com/register。 可选地,若要获取 hCaptcha 的原始响应,需调用 hcaptcha.Get(ctx)。示例:gohcaptchaResp, ok := hcaptcha.Get(ctx) if !ok { ctx.StatusCode(iris.StatusUnauthorized) ctx.WriteString("你是机器人吗?") return } ctx.Writef("在此执行注册操作...请求由人类发起。\n响应值为: %#+v", hcaptchaResp)1
2
3
4
5
6
7
8
就是这么简单!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
访问 http://yourdomain.com/register。 可选地,若要获取 hCaptcha 的原始响应,需调用 hcaptcha.Get(ctx)。示例:gohcaptchaResp, ok := hcaptcha.Get(ctx) if !ok { ctx.StatusCode(iris.StatusUnauthorized) ctx.WriteString("你是机器人吗?") return } ctx.Writef("在此执行注册操作...请求由人类发起。\n响应值为: %#+v", hcaptchaResp)1
2
3
4
5
6
7
8
就是这么简单!