最近有在用一款好玩的驗(yàn)證碼產(chǎn)品,樂(lè)于探索的我,決定從不同的語(yǔ)言去探索這款驗(yàn)證碼。
KgCaptcha支持PHP、Python、Java、C#的接入。下面是我接入過(guò)程記錄中的代碼。
<script src="captcha.js?appid=xxx"></script>
<script>
kg.captcha({
// 綁定元素,驗(yàn)證框顯示區(qū)域
bind: "#captchaBox",
// 驗(yàn)證成功事務(wù)處理
success: function(e) {
console.log(e);
},
// 驗(yàn)證失敗事務(wù)處理
failure: function(e) {
console.log(e);
},
// 點(diǎn)擊刷新按鈕時(shí)觸發(fā)
refresh: function(e) {
console.log(e);
}
});
</script>
<div id="captchaBox">載入中 ...</div>
<?php
include "public/KgCaptchaSDK.php";
// 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
$appId = "xxx";
// 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
$appSecret = "xxx";
$request = new kgCaptcha($appId, $appSecret);
// 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
$request->appCdn = "https://cdn.kgcaptcha.com";
// 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
$request->token = $_POST["kgCaptchaToken"];
// 當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě)
$request->userId = "kgCaptchaDemo";
// 請(qǐng)求超時(shí)時(shí)間,秒
$request->connectTimeout = 10;
$requestResult = $request->sendRequest();
if ($requestResult->code === 0) {
// 驗(yàn)簽成功邏輯處理
echo "驗(yàn)證通過(guò)";
} else {
// 驗(yàn)簽失敗邏輯處理
echo "驗(yàn)證失敗,錯(cuò)誤代碼:{$requestResult->code}, 錯(cuò)誤信息:{$requestResult->msg}";
}
from wsgiref.simple_server import make_server
from KgCaptchaSDK import KgCaptcha
def start(environ, response):
# 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
AppID = "xxx"
# 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
AppSecret = "xxx"
request = KgCaptcha(AppID, AppSecret)
# 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
request.appCdn = "https://cdn.kgcaptcha.com"
# 請(qǐng)求超時(shí)時(shí)間,秒
request.connectTimeout = 10
# 用戶id/登錄名/手機(jī)號(hào)等信息,當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě)
request.userId = "kgCaptchaDemo"
# 使用其它 WEB 框架時(shí)請(qǐng)刪除 request.parse,使用框架提供的方法獲取以下相關(guān)參數(shù)
parseEnviron = request.parse(environ)
# 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
request.token = parseEnviron["post"].get("kgCaptchaToken", "") # 前端 _POST["kgCaptchaToken"]
# 客戶端IP地址
request.clientIp = parseEnviron["ip"]
# 客戶端瀏覽器信息
request.clientBrowser = parseEnviron["browser"]
# 來(lái)路域名
request.domain = parseEnviron["domain"]
# 發(fā)送請(qǐng)求
requestResult = request.sendRequest()
if requestResult.code == 0:
# 驗(yàn)證通過(guò)邏輯處理
html = "驗(yàn)證通過(guò)"
else:
# 驗(yàn)證失敗邏輯處理
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) # 設(shè)置調(diào)試端口 http://localhost:8088/
httpd.serve_forever()
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");
// 后臺(tái)處理
if (request.getMethod().equals("POST")){
String html, appId, appSecret;
// 設(shè)置 AppId 及 AppSecret,在應(yīng)用管理中獲取
appId = "xxx";
appSecret = "xxx";
KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret);
// 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
KgRequest.token = request.getParameter("kgCaptchaToken");
// 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
KgRequest.appCdn = "https://cdn.kgcaptcha.com";
// 請(qǐng)求超時(shí)時(shí)間,秒
KgRequest.connectTimeout = 5;
// 用戶登錄或嘗試帳號(hào),當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
// 可以填寫(xiě)用戶輸入的登錄帳號(hào)(如:request.getParameter("username"),可攔截同一帳號(hào)多次嘗試等行為
KgRequest.userId = "kgCaptchaDemo";
// request 對(duì)象,當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
KgRequest.request = request;
// java 環(huán)境中無(wú)法提供 request 對(duì)象,請(qǐng)分別定義:clientIp|clientBrowser|domain 參數(shù),即:
// 發(fā)送驗(yàn)證請(qǐng)求
Map<String, String> requestResult = KgRequest.sendRequest();
if("0".toString().equals(requestResult.get("code"))) {
// 驗(yàn)簽成功邏輯處理 ***
// 這里做驗(yàn)證通過(guò)后的數(shù)據(jù)處理
// 如登錄/注冊(cè)場(chǎng)景,這里通常查詢數(shù)據(jù)庫(kù)、校驗(yàn)密碼、進(jìn)行登錄或注冊(cè)等動(dòng)作處理
// 如短信場(chǎng)景,這里可以開(kāi)始向用戶發(fā)送短信等動(dòng)作處理
// ...
html = "<script>alert(′驗(yàn)證通過(guò)′);history.back();</script>";
} else {
// 驗(yàn)簽失敗邏輯處理
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);
}
}
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){ // 有數(shù)據(jù)處理
// 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
appId = "xxx";
// 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
appSecret = "xxx";
var request = new kgCaptcha(appId, appSecret);
// 前端驗(yàn)證成功后頒發(fā)的 token,有效期兩分鐘
request.token = Request.Form["kgCaptchaToken"];
// 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
request.appCdn = "https://cdn.kgcaptcha.com";
// 當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
// 可以填寫(xiě)用戶輸入的登錄帳號(hào)(如:Request.Form["username"]),可攔截同一帳號(hào)多次嘗試等行為
request.userId = "kgCaptchaDemo";
// 請(qǐng)求超時(shí)時(shí)間,秒
request.connectTimeout = 5;
// 發(fā)送驗(yàn)證請(qǐng)求
var requestResult = request.sendRequest();
if (requestResult.code == 0) {
// 驗(yàn)簽成功邏輯處理 ***
// 這里做驗(yàn)證通過(guò)后的數(shù)據(jù)處理
// 如登錄/注冊(cè)場(chǎng)景,這里通常查詢數(shù)據(jù)庫(kù)、校驗(yàn)密碼、進(jìn)行登錄或注冊(cè)等動(dòng)作處理
// 如短信場(chǎng)景,這里可以開(kāi)始向用戶發(fā)送短信等動(dòng)作處理
// ...
html = "<script>alert(′驗(yàn)證通過(guò)′);history.back();</script>";
} else {
// 驗(yàn)簽失敗邏輯處理
html = "<script>alert(\"" + requestResult.msg + " - " + requestResult.code + "\");history.back();</script>";
}
// 輸出結(jié)果
Response.Write(html);
}
Response.Redirect("index.html");
}
}
SDK開(kāi)源地址:KgCaptcha (KgCaptcha) · GitHub,順便做了一個(gè)演示:凱格行為驗(yàn)證碼在線體驗(yàn)
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)