用户
 找回密码
 入住 CI 中国社区
搜索
查看: 239|回复: 0
收起左侧

【记】滑动拼图验证码在搜索中的作用

[复制链接]
发表于 2023-4-13 10:27:41 | 显示全部楼层 |阅读模式
本帖最后由 宙哈哈 于 2023-4-13 10:27 编辑

开头
验证码应用于我们生活、工作的方方面面,比如注册登录账号、支付订单、修改密码等。下面我是在一次项目中利用滑动拼图验证码和搜索功能“合作共赢”的记录。

验证码展示
1.PNG



具体实现
前端代码
HTML复制代码
// 引入js
 
<script src="captcha.js?appid=XXX"></script>
 
<script>
 
kg.captcha({
    // 绑定弹窗按钮
    button: "#captchaButton",
    // 验证成功事务处理
    success: function (e) {
        // 验证成功,直接提交表单
        // form1.submit();
        console.log(e);
    },
    // 验证失败事务处理
    failure: function (e) {
        console.log(e);
    },
    // 点击刷新按钮时触发
    refresh: function (e) {
        console.log(e);
    }
});
</script>
<a id="captchaButton"></a>
复制代码

验证结果说明
2.PNG

字段名 数据类型 描述
codenumber 返回code信息
msgstring 验证结果信息
rid
number
用户的验证码应用id
sensenumber 是否开启无感验证,0-关闭,1-开启
tokenstring 验证成功才有:token
weight number 错误严重性,0正常错误,可以继续操作,1一般错误,刷新/重新加载拼图,2严重错误,错误次数过多拒绝访问


Python代码
  1. from wsgiref.simple_server import make_server
  2. from KgCaptchaSDK import KgCaptcha
  3. def start(environ, response):
  4.     # 填写你的 AppId,在应用管理中获取
  5.     AppID = &quot;AppId&quot;
  6.     # 填写你的 AppSecret,在应用管理中获取
  7.     AppSecret = &quot;AppSecret&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()
复制代码

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


本版积分规则