开头的话
最近有在用一款好玩的验证码产品,乐于探索的我,决定从不同的语言去探索这款验证码。
KgCaptcha支持PHP、Python、Java、C#的接入。下面是我接入过程记录中的代码。

HTML
1<script src="captcha.js?appid=xxx"></script> 2<script> 3kg.captcha({ 4 // 绑定元素,验证框显示区域 5 bind: "#captchaBox", 6 // 验证成功事务处理 7 success: function(e) { 8 console.log(e); 9 }, 10 // 验证失败事务处理 11 failure: function(e) { 12 console.log(e); 13 }, 14 // 点击刷新按钮时触发 15 refresh: function(e) { 16 console.log(e); 17 } 18}); 19</script> 20<div id="captchaBox">载入中 ...</div>
PHP
1<?php 2include "public/KgCaptchaSDK.php"; 3// 填写你的 AppId,在应用管理中获取 4$appId = "xxx"; 5// 填写你的 AppSecret,在应用管理中获取 6$appSecret = "xxx"; 7$request = new kgCaptcha($appId, $appSecret); 8// 填写应用服务域名,在应用管理中获取 9$request->appCdn = "https://cdn.kgcaptcha.com"; 10// 前端验证成功后颁发的 token,有效期为两分钟 11$request->token = $_POST["kgCaptchaToken"]; 12// 当安全策略中的防控等级为3时必须填写 13$request->userId = "kgCaptchaDemo"; 14// 请求超时时间,秒 15$request->connectTimeout = 10; 16$requestResult = $request->sendRequest(); 17if ($requestResult->code === 0) { 18 // 验签成功逻辑处理 19 echo "验证通过"; 20} else { 21 // 验签失败逻辑处理 22 echo "验证失败,错误代码:{$requestResult->code}, 错误信息:{$requestResult->msg}"; 23}
Python
1from wsgiref.simple_server import make_server 2from KgCaptchaSDK import KgCaptcha 3def start(environ, response): 4 # 填写你的 AppId,在应用管理中获取 5 AppID = "xxx" 6 # 填写你的 AppSecret,在应用管理中获取 7 AppSecret = "xxx" 8 request = KgCaptcha(AppID, AppSecret) 9 # 填写应用服务域名,在应用管理中获取 10 request.appCdn = "https://cdn.kgcaptcha.com" 11 # 请求超时时间,秒 12 request.connectTimeout = 10 13 # 用户id/登录名/手机号等信息,当安全策略中的防控等级为3时必须填写 14 request.userId = "kgCaptchaDemo" 15 # 使用其它 WEB 框架时请删除 request.parse,使用框架提供的方法获取以下相关参数 16 parseEnviron = request.parse(environ) 17 # 前端验证成功后颁发的 token,有效期为两分钟 18 request.token = parseEnviron["post"].get("kgCaptchaToken", "") # 前端 _POST["kgCaptchaToken"] 19 # 客户端IP地址 20 request.clientIp = parseEnviron["ip"] 21 # 客户端浏览器信息 22 request.clientBrowser = parseEnviron["browser"] 23 # 来路域名 24 request.domain = parseEnviron["domain"] 25 # 发送请求 26 requestResult = request.sendRequest() 27 if requestResult.code == 0: 28 # 验证通过逻辑处理 29 html = "验证通过" 30 else: 31 # 验证失败逻辑处理 32 html = f"{requestResult.msg} - {requestResult.code}" 33 response("200 OK", [("Content-type", "text/html; charset=utf-8")]) 34 return [bytes(str(html), encoding="utf-8")] 35 36httpd = make_server("0.0.0.0", 8088, start) # 设置调试端口 http://localhost:8088/ 37httpd.serve_forever()
Java
1package com.kyger; 2import jakarta.servlet.ServletException; 3import jakarta.servlet.http.HttpServlet; 4import jakarta.servlet.http.HttpServletRequest; 5import jakarta.servlet.http.HttpServletResponse; 6import java.io.IOException; 7import java.util.Map; 8public class demo extends HttpServlet { 9 private static final long serialVersionUID = 1L; 10 public demo() { 11 super(); 12 } 13 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 // 编码 15 request.setCharacterEncoding("utf-8"); 16 response.setCharacterEncoding("utf-8");; 17 response.setContentType("text/html; charset=utf-8"); 18 // 后台处理 19 if (request.getMethod().equals("POST")){ 20 String html, appId, appSecret; 21 // 设置 AppId 及 AppSecret,在应用管理中获取 22 appId = "xxx"; 23 appSecret = "xxx"; 24 KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret); 25 // 前端验证成功后颁发的 token,有效期为两分钟 26 KgRequest.token = request.getParameter("kgCaptchaToken"); 27 // 填写应用服务域名,在应用管理中获取 28 KgRequest.appCdn = "https://cdn.kgcaptcha.com"; 29 // 请求超时时间,秒 30 KgRequest.connectTimeout = 5; 31 // 用户登录或尝试帐号,当安全策略中的防控等级为3时必须填写,一般情况下可以忽略 32 // 可以填写用户输入的登录帐号(如:request.getParameter("username"),可拦截同一帐号多次尝试等行为 33 KgRequest.userId = "kgCaptchaDemo"; 34 // request 对象,当安全策略中的防控等级为3时必须填写,一般情况下可以忽略 35 KgRequest.request = request; 36 // java 环境中无法提供 request 对象,请分别定义:clientIp¦clientBrowser¦domain 参数,即: 37 38 // 发送验证请求 39 Map<String, String> requestResult = KgRequest.sendRequest(); 40 if("0".toString().equals(requestResult.get("code"))) { 41 // 验签成功逻辑处理 *** 42 // 这里做验证通过后的数据处理 43 // 如登录/注册场景,这里通常查询数据库、校验密码、进行登录或注册等动作处理 44 // 如短信场景,这里可以开始向用户发送短信等动作处理 45 // ... 46 html = "<script>alert(´验证通过´);history.back();</script>"; 47 } else { 48 // 验签失败逻辑处理 49 html = "<script>alert(\"" + requestResult.get("msg") + " - " + requestResult.get("code") + "\");history.back();</script>"; 50 } 51 response.getWriter().append(html); 52 } else { 53 response.sendRedirect("index.html"); 54 } 55 } 56 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 57 doGet(request, response); 58 } 59}
C#
1using System; 2using KgCaptchaSDK; 3public partial class _Default : System.Web.UI.Page{ 4 protected void Page_Load(object sender, EventArgs e) { 5 // 后端处理 6 string html, appId, appSecret, Token; 7 if (Request.Form.ToString().Length > 0){ // 有数据处理 8 // 填写你的 AppId,在应用管理中获取 9 appId = "xxx"; 10 // 填写你的 AppSecret,在应用管理中获取 11 appSecret = "xxx"; 12 var request = new kgCaptcha(appId, appSecret); 13 // 前端验证成功后颁发的 token,有效期两分钟 14 request.token = Request.Form["kgCaptchaToken"]; 15 // 填写应用服务域名,在应用管理中获取 16 request.appCdn = "https://cdn.kgcaptcha.com"; 17 // 当安全策略中的防控等级为3时必须填写,一般情况下可以忽略 18 // 可以填写用户输入的登录帐号(如:Request.Form["username"]),可拦截同一帐号多次尝试等行为 19 request.userId = "kgCaptchaDemo"; 20 // 请求超时时间,秒 21 request.connectTimeout = 5; 22 // 发送验证请求 23 var requestResult = request.sendRequest(); 24 if (requestResult.code == 0) { 25 // 验签成功逻辑处理 *** 26 // 这里做验证通过后的数据处理 27 // 如登录/注册场景,这里通常查询数据库、校验密码、进行登录或注册等动作处理 28 // 如短信场景,这里可以开始向用户发送短信等动作处理 29 // ... 30 html = "<script>alert(´验证通过´);history.back();</script>"; 31 } else { 32 // 验签失败逻辑处理 33 html = "<script>alert(\"" + requestResult.msg + " - " + requestResult.code + "\");history.back();</script>"; 34 } 35 // 输出结果 36 Response.Write(html); 37 } 38 Response.Redirect("index.html"); 39 } 40}
最后
SDK开源地址:KgCaptcha (KgCaptcha) · GitHub,顺便做了一个演示:凯格行为验证码在线体验