收藏本站 Archiver
    请 后使用快捷导航
    没有账号?入住 CI 中国社区
    查看: 3690|回复: 0

    [Web] KgCaptcha验证码实现笔记

    [复制链接]
    楼主
    发表于 2023年4月11日 11:09:25 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |
    本帖最后由 宙哈哈 于 2023年4月11日 11:09 编辑

    背景
    闲来无聊,在网上发现了一个验证码产品KgCaptcha,下面是我用KgCaptcha开发验证码的记录。

    Web接入
    访问官网,注册账号后登录控制台,创建应用,系统会分配一个唯一的AppId、AppSecret。

    2.PNG (36.64 KB, 下载次数: 90)

    下载附件

    2023年4月11日 11:05 上传




    引入JS
    这里的appid在用户控制台获取。
    HTML复制代码
    <script src="captcha.js?appid=xxx"></script>
    复制代码

    JS接入代码
    HTML复制代码
    <script>

    kg.captcha({

    // 绑定元素,验证框显示区域

    bind: "#captchaBox",

    // 验证成功事务处理

    success: function(e) {

    console.log(e);

    },

    // 验证失败事务处理

    failure: function(e) {

    console.log(e);

    },

    // 点击刷新按钮时触发

    refresh: function(e) {

    console.log(e);
    }

    });


    </script>
    复制代码

    Python后台验证
    1. from wsgiref.simple_server import make_server

    2. from KgCaptchaSDK import KgCaptcha

    3. def start(environ, response):

    4. # 填写你的 AppId,在应用管理中获取

    5. AppID = &quot;xxx&quot;

    6. # 填写你的 AppSecret,在应用管理中获取

    7. AppSecret = &quot;xxx&quot;

    8. request = KgCaptcha(AppID, AppSecret)

    9. # 填写应用服务域名,在应用管理中获取

    10. request.appCdn = &quot;https://cdn.kgcaptcha.com&quot;

    11. # 请求超时时间,秒

    12. request.connectTimeout = 10

    13. # 用户id/登录名/手机号等信息,当安全策略中的防控等级为3时必须填写

    14. request.userId = &quot;kgCaptchaDemo&quot;

    15. # 使用其它 WEB 框架时请删除 request.parse,使用框架提供的方法获取以下相关参数

    16. parseEnviron = request.parse(environ)

    17. # 前端验证成功后颁发的 token,有效期为两分钟

    18. request.token = parseEnviron[&quot;post&quot;].get(&quot;kgCaptchaToken&quot;, &quot;&quot;) # 前端 _POST[&quot;kgCaptchaToken&quot;]

    19. # 客户端IP地址

    20. request.clientIp = parseEnviron[&quot;ip&quot;]

    21. # 客户端浏览器信息

    22. request.clientBrowser = parseEnviron[&quot;browser&quot;]

    23. # 来路域名

    24. request.domain = parseEnviron[&quot;domain&quot;]

    25. # 发送请求

    26. requestResult = request.sendRequest()

    27. if requestResult.code == 0:

    28. # 验证通过逻辑处理

    29. html = &quot;验证通过&quot;

    30. else:

    31. # 验证失败逻辑处理

    32. html = f&quot;{requestResult.msg} - {requestResult.code}&quot;

    33. response(&quot;200 OK&quot;, [(&quot;Content-type&quot;, &quot;text/html; charset=utf-8&quot;)])

    34. return [bytes(str(html), encoding=&quot;utf-8&quot;)]

    35. httpd = make_server(&quot;0.0.0.0&quot;, 8088, start) # 设置调试端口 http://localhost:8088/


    36. httpd.serve_forever()
    复制代码

    效果展示

    1.PNG (210.92 KB, 下载次数: 111)

    下载附件

    2023年4月11日 11:06 上传



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

    返回列表
    您需要登录后才可以回帖 登录 | 入住 CI 中国社区

    本版积分规则

    AltStyle によって変換されたページ (->オリジナル) /