【笔记】CloudflareTurnstile 学习笔记
source link: https://loli.fj.cn/2024/04/29/CloudflareTurnstile%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【笔记】CloudflareTurnstile 学习笔记
2024-04-29 2024-04-30
2
CloudflareTurnstile 学习笔记
- 访问 Cloudflare 仪表盘 https://dash.cloudflare.com/->
Turnstile
->添加站点
- 定义站点名称和域名 ->
创建
- 复制
站点密钥
和密钥
- 发送请求给 Cloudflare 获取 token
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"></script>
<div id="example-container">
<script>
turnstile.ready(function () {
turnstile.render('#example-container', {
sitekey: '站点密钥',
callback: function(token) {
console.log(`Challenge Success ${token}`);
},
});
});
</script>
- 把从 Cloudflare 获取 token 发送请求给后端
POST http://localhost:8080/api
Content-Type: application/json
{username: "", password: "", token: "从Cloudflare获取的token"}
- 发送请求给 Cloudflare 验证从前端获取的 token
POST https://challenges.cloudflare.com/turnstile/v0/siteverify
Content-Type: application/json
{secret: "密钥", response: "从前端获取的token"}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type CloudflareTurnstileVerificationResponseEntity struct {
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
ChallengeTS string `json:"challenge_ts"`
Hostname string `json:"hostname"`
Action string `json:"action"`
Cdata string `json:"cdata"`
Messages []string `json:"messages"`
}
func CloudflareTurnstileVerification(token string) (err error, success bool) {
var url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
var secret = "密钥"
// 请求体参数
var requestPayloadMap map[string]string = map[string]string{
"secret": secret,
"response": token,
}
var requestPayloadString string
if res, err := json.Marshal(requestPayloadMap); err != nil {
return err, success
} else {
requestPayloadString = string(res)
}
// 发起请求获取响应
var responseEntity *http.Response
if res, err := http.Post(url, "application/json", strings.NewReader(requestPayloadString)); err != nil {
return err, success
} else {
responseEntity = res
}
defer responseEntity.Body.Close()
// 将响应体转换为文本
var responseText []byte
if res, err := ioutil.ReadAll(responseEntity.Body); err != nil {
return err, success
} else {
responseText = res
}
// 将JSON格式的响应体文本转换为响应体结构体
var responseObject CloudflareTurnstileVerificationResponseEntity
if err := json.Unmarshal(responseText, &responseObject); err != nil {
return err, success
}
if responseObject.Success == false {
var errorCodeListString strings.Builder
errorCodeListString.WriteString("\nErrorCodes:\n")
for i := 0; i < len(responseObject.ErrorCodes); i++ {
errorCodeListString.WriteString(fmt.Sprintf("%d:%s\n", i, responseObject.ErrorCodes[i]))
}
errorCodeListString.WriteString("\nMessages:\n")
for i := 0; i < len(responseObject.Messages); i++ {
errorCodeListString.WriteString(fmt.Sprintf("%d:%s\n", i, responseObject.Messages[i]))
}
return err, false
}
return err, true
}
Recommend
-
114
门票股学习笔记1-旅游行业-求点评,赠收费数据 - 被迫进入权益类投资,小白重头学起,还请各位老股民们不吝赐教。先奉上赠品:凡是在本帖留言,提供有价值的点评/建议/推荐,并获得我点赞的,均可得到参与奖一份,奖品为集思录定增数据两个月使用权限或转债增强数...
-
99
作者: 一字马胡 转载标志 【2017-12-15】 更新日志 导入 在RxJava学习笔记系列的第一篇文章中,我分析了RxJava中核心的两个对象Observable和Observer,并且分析了Observer是如何实现订阅一个Observable的,作为学习RxJava的第一篇分析总结,内容较为浅显,本文将...
-
137
安装1brew install parallel 官方文档指南1man parallel_tutorial 输入源直接在命令行传递参数1::: 参数1 ::: 参数2 12345678910111213141516171819202122$ echo ::: a b c ebace$ parallel echo ::: a b c e ::: f g j ia ga ja ib fb jb ib
-
89
本文是阿铭linux系列课程关于iptables规则备份和恢复,firewalld的9个zone,firewalld关于zone和service的操作的学习笔记.
-
91
8.1什么是sqlSQL英文全称是StructuredQueryLanguage,中文意思是结构化查询语言。它是一种对关系型数据库中的数据进行定义和操作的语言方法。是大多数关系数据库管理系统所支持的工业标准。8.1SQL的分类SQL结构化查询语言包含6部分。一、数据查询语言(DQL)DQL全称...
-
84
2.1 2.2 系统目录结构
-
67
9.8创建Mysql用户及赋予用户权限9.8.1通过help查看grant命令帮助1.通过在mysql中输入“helpgrant”得到如下帮助信息。mysql>helpgrant;……省略部分…….CREATEUSER'jeffrey'@'localhost'IDENTIFIEDBY'mypass';GRANTALLONdb1.*TO'jeffrey'@'localhost';GRANTSE
-
99
2.1/2.2 系统目录结构tree树结构,显示目录结构;安装tree命令#yum install -y tree;2层显示树结构#tree -L 2;tree常用选项#tree --hap;tree的详细用法#man tree;/bin,/sbin,/usr/bin,/usr/sbin存放命令,sbin为root用户用,bin普通用户用;PATH环境变量;/...
-
44
2.3:ls命令 2.4:文件类型
-
43
2.5.alias命令
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK