结尾的话

最近有在用一款好玩的验证码产品,乐于摸索的我,决定从不同的语言去摸索这款验证码。

KgCaptcha反对PHP、Python、Java、C#的接入。上面是我接入过程记录中的代码。

HTML

<script src="captcha.js?appid=xxx"></script><script>kg.captcha({    // 绑定元素,验证框显示区域    bind: "#captchaBox",    // 验证胜利事务处理    success: function(e) {        console.log(e);    },    // 验证失败事务处理    failure: function(e) {        console.log(e);    },    // 点击刷新按钮时触发    refresh: function(e) {        console.log(e);    }});</script><div id="captchaBox">载入中 ...</div>

PHP

<?phpinclude "public/KgCaptchaSDK.php";// 填写你的 AppId,在利用治理中获取$appId = "xxx";// 填写你的 AppSecret,在利用治理中获取$appSecret = "xxx";$request = new kgCaptcha($appId, $appSecret);// 填写应用服务域名,在利用治理中获取$request->appCdn = "appCdn";// 前端验证胜利后颁发的 token,有效期为两分钟$request->token = $_POST["kgCaptchaToken"];// 当安全策略中的防控等级为3时必须填写$request->userId = "kgCaptchaDemo";// 申请超时工夫,秒$request->connectTimeout = 10;$requestResult = $request->sendRequest();if ($requestResult->code === 0) {    // 验签胜利逻辑解决    echo "验证通过";} else {    // 验签失败逻辑解决    echo "验证失败,错误代码:{$requestResult->code}, 错误信息:{$requestResult->msg}";}

Python

from wsgiref.simple_server import make_serverfrom KgCaptchaSDK import KgCaptchadef start(environ, response):    # 填写你的 AppId,在利用治理中获取    AppID = "xxx"    # 填写你的 AppSecret,在利用治理中获取    AppSecret = "xxx"    request = KgCaptcha(AppID, AppSecret)    # 填写应用服务域名,在利用治理中获取    request.appCdn = "appCdn"    # 申请超时工夫,秒    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_foreve

Java

package com.kyger;import jakarta.servlet.ServletException;import jakarta.servlet.http.HttpServlet;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.Map;public class demo extends HttpServlet {    private static final long serialVersionUID = 1L;    public demo() {        super();    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // 编码        request.setCharacterEncoding("utf-8");        response.setCharacterEncoding("utf-8");;        response.setContentType("text/html; charset=utf-8");        // 后盾解决        if (request.getMethod().equals("POST")){            String html, appId, appSecret;            // 设置 AppId 及 AppSecret,在利用治理中获取            appId = "xxx";            appSecret = "xxx";            KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret);            // 前端验证胜利后颁发的 token,有效期为两分钟            KgRequest.token = request.getParameter("kgCaptchaToken");            // 填写应用服务域名,在利用治理中获取            KgRequest.appCdn = "appCdn";            // 申请超时工夫,秒            KgRequest.connectTimeout = 5;            // 用户登录或尝试帐号,当安全策略中的防控等级为3时必须填写,个别状况下能够疏忽            // 能够填写用户输出的登录帐号(如:request.getParameter("username"),可拦挡同一帐号屡次尝试等行为            KgRequest.userId = "kgCaptchaDemo";            // request 对象,当安全策略中的防控等级为3时必须填写,个别状况下能够疏忽            KgRequest.request = request;            // java 环境中无奈提供 request 对象,请别离定义:clientIp¦clientBrowser¦domain 参数,即:            // 发送验证申请            Map<String, String> requestResult = KgRequest.sendRequest();            if("0".toString().equals(requestResult.get("code"))) {                // 验签胜利逻辑解决 ***                // 这里做验证通过后的数据处理                // 如登录/注册场景,这里通常查询数据库、校验明码、进行登录或注册等动作解决                // 如短信场景,这里能够开始向用户发送短信等动作解决                // ...                html = "<script>alert(´验证通过´);history.back();</script>";            } else {                // 验签失败逻辑解决                html = "<script>alert(\"" + requestResult.get("msg") + " - " + requestResult.get("code") + "\");history.back();</script>";            }            response.getWriter().append(html);        } else {            response.sendRedirect("index.html");        }    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}

C#.Net

using System;using KgCaptchaSDK;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e) {        // 后端解决        string html, appId, appSecret, Token;        if (Request.Form.ToString().Length > 0){  // 有数据处理            // 填写你的 AppId,在利用治理中获取            appId = "xxx";            // 填写你的 AppSecret,在利用治理中获取            appSecret = "xxx";            var request = new kgCaptcha(appId, appSecret);            // 前端验证胜利后颁发的 token,有效期两分钟            request.token = Request.Form["kgCaptchaToken"];            // 填写应用服务域名,在利用治理中获取            request.appCdn = "appCdn";            // 当安全策略中的防控等级为3时必须填写,个别状况下能够疏忽            // 能够填写用户输出的登录帐号(如:Request.Form["username"]),可拦挡同一帐号屡次尝试等行为            request.userId = "kgCaptchaDemo";            // 申请超时工夫,秒            request.connectTimeout = 5;            // 发送验证申请            var requestResult = request.sendRequest();            if (requestResult.code == 0) {                // 验签胜利逻辑解决 ***                // 这里做验证通过后的数据处理                // 如登录/注册场景,这里通常查询数据库、校验明码、进行登录或注册等动作解决                // 如短信场景,这里能够开始向用户发送短信等动作解决                // ...                html = "<script>alert(´验证通过´);history.back();</script>";            } else {                // 验签失败逻辑解决                html = "<script>alert(\"" + requestResult.msg + " - " + requestResult.code + "\");history.back();</script>";            }            // 输入后果            Response.Write(html);        }        Response.Redirect("index.html");    }}

最初

SDK开源地址:https://github.com/KgCaptcha,顺便做了一个演示:https://www.kgcaptcha.com/demo/