共计 1599 个字符,预计需要花费 4 分钟才能阅读完成。
背景
保障数据安全是当今互联网时代的重要工作。为了应答日益简单的网络攻击,行为验证码应运而生。行为验证码通过剖析用户在网站上的行为模式,辨认失常用户并阻止歹意流动。
它不仅提供了更弱小的身份确认形式,还能无效缩小伪造身份和破解账户的危险。从行为验证码开始,咱们能够为数据安全建设起松软的防线。
前端代码
<script src="https://cdn6.kgcaptcha.com/captcha.js"></script> | |
<script> | |
kg.captcha({ | |
// 绑定弹窗按钮 | |
button: "#captchaButton", | |
// 验证胜利事务处理 | |
success: function (e) { | |
// 验证胜利,间接提交表单 | |
// form1.submit(); | |
console.log(e); | |
}, | |
// 验证失败事务处理 | |
failure: function (e) {console.log(e); | |
}, | |
// 点击刷新按钮时触发 | |
refresh: function (e) {console.log(e); | |
} | |
}); | |
</script> | |
<a id="captchaButton"> 点击弹出验证窗口 </a> |
Python 代码
from wsgiref.simple_server import make_server | |
from KgCaptchaSDK import KgCaptcha | |
def start(environ, response): | |
# 填写你的 AppId,在利用治理中获取 | |
AppID = "AppID" | |
# 填写你的 AppSecret,在利用治理中获取 | |
AppSecret = "AppSecret" | |
request = KgCaptcha(AppID, AppSecret) | |
# 填写应用服务域名,在利用治理中获取 | |
request.appCdn = "https://cdn6.kgcaptcha.com" | |
# 申请超时工夫,秒 | |
request.connectTimeout = 10 | |
# 用户 id/ 登录名 / 手机号等信息,当安全策略中的防控等级为 3 时必须填写 | |
request.userId = "kgCaptchaDemo" | |
# 应用其它 WEB 框架时请删除 request.parse,应用框架提供的办法获取以下相干参数 | |
parseEnviron = request.parse(environ) | |
# 前端验证胜利后颁发的 token,有效期为两分钟 | |
request.token = parseEnviron["post"].get("kgCaptchaToken", "") # 前端 _POST["kgCaptchaToken"] | |
# 客户端 IP 地址 | |
request.clientIp = parseEnviron["ip"] | |
# 客户端浏览器信息 | |
request.clientBrowser = parseEnviron["browser"] | |
# 去路域名 | |
request.domain = parseEnviron["domain"] | |
# 发送申请 | |
requestResult = request.sendRequest() | |
if requestResult.code == 0: | |
# 验证通过逻辑解决 | |
html = "验证通过" | |
else: | |
# 验证失败逻辑解决 | |
html = f"{requestResult.msg} - {requestResult.code}" | |
response("200 OK", [("Content-type", "text/html; charset=utf-8")]) | |
return [bytes(str(html), encoding="utf-8")] | |
httpd = make_server("0.0.0.0", 8088, start) # 设置调试端口 http://localhost:8088/ | |
httpd.serve_forever() |
最初
SDK 开源地址:https://github.com/KgCaptcha,顺便做了一个演示:https://www.kgcaptcha.com/demo/
正文完