亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 16
粉絲 0
評(píng)論 0
訪問(wèn)量 9619
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
KgCaptcha接入?yún)R總
原創(chuàng)
469人瀏覽過(guò)

開(kāi)頭的話

最近有在用一款好玩的驗(yàn)證碼產(chǎn)品,樂(lè)于探索的我,決定從不同的語(yǔ)言去探索這款驗(yàn)證碼。

KgCaptcha支持PHP、Python、Java、C#的接入。下面是我接入過(guò)程記錄中的代碼。

HTML

  1. <script src="captcha.js?appid=xxx"></script>
  2. <script>
  3. kg.captcha({
  4. // 綁定元素,驗(yàn)證框顯示區(qū)域
  5. bind: "#captchaBox",
  6. // 驗(yàn)證成功事務(wù)處理
  7. success: function(e) {
  8. console.log(e);
  9. },
  10. // 驗(yàn)證失敗事務(wù)處理
  11. failure: function(e) {
  12. console.log(e);
  13. },
  14. // 點(diǎn)擊刷新按鈕時(shí)觸發(fā)
  15. refresh: function(e) {
  16. console.log(e);
  17. }
  18. });
  19. </script>
  20. <div id="captchaBox">載入中 ...</div>

PHP

  1. <?php
  2. include "public/KgCaptchaSDK.php";
  3. // 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
  4. $appId = "xxx";
  5. // 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
  6. $appSecret = "xxx";
  7. $request = new kgCaptcha($appId, $appSecret);
  8. // 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
  9. $request->appCdn = "https://cdn.kgcaptcha.com";
  10. // 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
  11. $request->token = $_POST["kgCaptchaToken"];
  12. // 當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě)
  13. $request->userId = "kgCaptchaDemo";
  14. // 請(qǐng)求超時(shí)時(shí)間,秒
  15. $request->connectTimeout = 10;
  16. $requestResult = $request->sendRequest();
  17. if ($requestResult->code === 0) {
  18. // 驗(yàn)簽成功邏輯處理
  19. echo "驗(yàn)證通過(guò)";
  20. } else {
  21. // 驗(yàn)簽失敗邏輯處理
  22. echo "驗(yàn)證失敗,錯(cuò)誤代碼:{$requestResult->code}, 錯(cuò)誤信息:{$requestResult->msg}";
  23. }

Python

  1. from wsgiref.simple_server import make_server
  2. from KgCaptchaSDK import KgCaptcha
  3. def start(environ, response):
  4. # 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
  5. AppID = "xxx"
  6. # 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
  7. AppSecret = "xxx"
  8. request = KgCaptcha(AppID, AppSecret)
  9. # 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
  10. request.appCdn = "https://cdn.kgcaptcha.com"
  11. # 請(qǐng)求超時(shí)時(shí)間,秒
  12. request.connectTimeout = 10
  13. # 用戶id/登錄名/手機(jī)號(hào)等信息,當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě)
  14. request.userId = "kgCaptchaDemo"
  15. # 使用其它 WEB 框架時(shí)請(qǐng)刪除 request.parse,使用框架提供的方法獲取以下相關(guān)參數(shù)
  16. parseEnviron = request.parse(environ)
  17. # 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
  18. request.token = parseEnviron["post"].get("kgCaptchaToken", "") # 前端 _POST["kgCaptchaToken"]
  19. # 客戶端IP地址
  20. request.clientIp = parseEnviron["ip"]
  21. # 客戶端瀏覽器信息
  22. request.clientBrowser = parseEnviron["browser"]
  23. # 來(lái)路域名
  24. request.domain = parseEnviron["domain"]
  25. # 發(fā)送請(qǐng)求
  26. requestResult = request.sendRequest()
  27. if requestResult.code == 0:
  28. # 驗(yàn)證通過(guò)邏輯處理
  29. html = "驗(yàn)證通過(guò)"
  30. else:
  31. # 驗(yàn)證失敗邏輯處理
  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. httpd = make_server("0.0.0.0", 8088, start) # 設(shè)置調(diào)試端口 http://localhost:8088/
  36. httpd.serve_forever()

Java

  1. package com.kyger;
  2. import jakarta.servlet.ServletException;
  3. import jakarta.servlet.http.HttpServlet;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.util.Map;
  8. public 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. // 后臺(tái)處理
  19. if (request.getMethod().equals("POST")){
  20. String html, appId, appSecret;
  21. // 設(shè)置 AppId 及 AppSecret,在應(yīng)用管理中獲取
  22. appId = "xxx";
  23. appSecret = "xxx";
  24. KgCaptchaSDK KgRequest = new KgCaptchaSDK(appId, appSecret);
  25. // 前端驗(yàn)證成功后頒發(fā)的 token,有效期為兩分鐘
  26. KgRequest.token = request.getParameter("kgCaptchaToken");
  27. // 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
  28. KgRequest.appCdn = "https://cdn.kgcaptcha.com";
  29. // 請(qǐng)求超時(shí)時(shí)間,秒
  30. KgRequest.connectTimeout = 5;
  31. // 用戶登錄或嘗試帳號(hào),當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
  32. // 可以填寫(xiě)用戶輸入的登錄帳號(hào)(如:request.getParameter("username"),可攔截同一帳號(hào)多次嘗試等行為
  33. KgRequest.userId = "kgCaptchaDemo";
  34. // request 對(duì)象,當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
  35. KgRequest.request = request;
  36. // java 環(huán)境中無(wú)法提供 request 對(duì)象,請(qǐng)分別定義:clientIp|clientBrowser|domain 參數(shù),即:
  37. // 發(fā)送驗(yàn)證請(qǐng)求
  38. Map<String, String> requestResult = KgRequest.sendRequest();
  39. if("0".toString().equals(requestResult.get("code"))) {
  40. // 驗(yàn)簽成功邏輯處理 ***
  41. // 這里做驗(yàn)證通過(guò)后的數(shù)據(jù)處理
  42. // 如登錄/注冊(cè)場(chǎng)景,這里通常查詢數(shù)據(jù)庫(kù)、校驗(yàn)密碼、進(jìn)行登錄或注冊(cè)等動(dòng)作處理
  43. // 如短信場(chǎng)景,這里可以開(kāi)始向用戶發(fā)送短信等動(dòng)作處理
  44. // ...
  45. html = "<script>alert(′驗(yàn)證通過(guò)′);history.back();</script>";
  46. } else {
  47. // 驗(yàn)簽失敗邏輯處理
  48. html = "<script>alert(\"" + requestResult.get("msg") + " - " + requestResult.get("code") + "\");history.back();</script>";
  49. }
  50. response.getWriter().append(html);
  51. } else {
  52. response.sendRedirect("index.html");
  53. }
  54. }
  55. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  56. doGet(request, response);
  57. }
  58. }

C

  1. using System;
  2. using KgCaptchaSDK;
  3. public 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){ // 有數(shù)據(jù)處理
  8. // 填寫(xiě)你的 AppId,在應(yīng)用管理中獲取
  9. appId = "xxx";
  10. // 填寫(xiě)你的 AppSecret,在應(yīng)用管理中獲取
  11. appSecret = "xxx";
  12. var request = new kgCaptcha(appId, appSecret);
  13. // 前端驗(yàn)證成功后頒發(fā)的 token,有效期兩分鐘
  14. request.token = Request.Form["kgCaptchaToken"];
  15. // 填寫(xiě)應(yīng)用服務(wù)域名,在應(yīng)用管理中獲取
  16. request.appCdn = "https://cdn.kgcaptcha.com";
  17. // 當(dāng)安全策略中的防控等級(jí)為3時(shí)必須填寫(xiě),一般情況下可以忽略
  18. // 可以填寫(xiě)用戶輸入的登錄帳號(hào)(如:Request.Form["username"]),可攔截同一帳號(hào)多次嘗試等行為
  19. request.userId = "kgCaptchaDemo";
  20. // 請(qǐng)求超時(shí)時(shí)間,秒
  21. request.connectTimeout = 5;
  22. // 發(fā)送驗(yàn)證請(qǐng)求
  23. var requestResult = request.sendRequest();
  24. if (requestResult.code == 0) {
  25. // 驗(yàn)簽成功邏輯處理 ***
  26. // 這里做驗(yàn)證通過(guò)后的數(shù)據(jù)處理
  27. // 如登錄/注冊(cè)場(chǎng)景,這里通常查詢數(shù)據(jù)庫(kù)、校驗(yàn)密碼、進(jìn)行登錄或注冊(cè)等動(dòng)作處理
  28. // 如短信場(chǎng)景,這里可以開(kāi)始向用戶發(fā)送短信等動(dòng)作處理
  29. // ...
  30. html = "<script>alert(′驗(yàn)證通過(guò)′);history.back();</script>";
  31. } else {
  32. // 驗(yàn)簽失敗邏輯處理
  33. html = "<script>alert(\"" + requestResult.msg + " - " + requestResult.code + "\");history.back();</script>";
  34. }
  35. // 輸出結(jié)果
  36. Response.Write(html);
  37. }
  38. Response.Redirect("index.html");
  39. }
  40. }

最后

SDK開(kāi)源地址:KgCaptcha (KgCaptcha) · GitHub,順便做了一個(gè)演示:凱格行為驗(yàn)證碼在線體驗(yàn)

本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)