• python极验验证部署


    1、官方下载文档

      https://github.com/GeeTeam/gt3-dotnet-sdk

    2、下载极验官方geetest包

    pip install geetest

    3、根据不同开发环境选择对应文档包,以Django为例:

      Ajax方式验证:

    <script>
        var handlerPopup = function (captchaObj) {
            captchaObj.onSuccess(function () {
                var validate = captchaObj.getValidate();
                var $spanEle = $('.error')
                $.ajax({
                    url: "/login_geetest/",  //第二次校验,验证用户名密码
                    type: "post",
                    dataType: "json",
                    data: {
                        username: $('#inputUser').val(),
                        password: $('#inputPassword').val(),
                        geetest_challenge: validate.geetest_challenge,
                        geetest_validate: validate.geetest_validate,
                        geetest_seccode: validate.geetest_seccode
                    },
                    success: function (msg) {
                        if (msg.state == 1) {
                            $spanEle.text(msg.error)
                            $spanEle.parent().parent().addClass(msg.class)
                        } else {
                            {##结合auth模块的登录校验装饰器,获取需要访问url路由地址,实现登陆成功后,重定向到该地址#}
                            var url = location.search
                            if (url.indexOf("?") != -1) {
                                var str = url.substr(1);
                                strs = str.split("=");
                                location.href = strs[1];
                            } else {
                                location.href = '/index/';
                            }
                        }
    
                    }
                });
            });
            $("#login").click(function () {
                captchaObj.show();
            });
            // 将验证码加到id为captcha的元素里
            captchaObj.appendTo("#popup-captcha");
        };
        // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback)
        $.ajax({
            url: "/geetest/register?t=" + (new Date()).getTime(), // 加随机数防止缓存, 第一次校验,验证极验图片
            type: "get",
            dataType: "json",
            success: function (data) {
                // 使用initGeetest接口
                // 参数1:配置参数
                // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
                initGeetest({
                    gt: data.gt,
                    challenge: data.challenge,
                    product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
                    offline: !data.success, // 表示用户后台检测极验服务器是否宕机,一般不需要关注
                    new_captcha: data.new_captcha
                }, handlerPopup);
            }
        });
    </script>
    HTML
    urlpatterns = [
        url(r'^login_geetest/', views.login_geetest),  # 极验验证登录
        url(r'^geetest/register', views.pcgetcaptcha),  # 极验验证的图片视图
    ]
    urls.py
    # 处理极验 获取验证码的视图
    def pcgetcaptcha(request):
        user_id = 'test'
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        status = gt.pre_process(user_id)
        request.session[gt.GT_STATUS_SESSION_KEY] = status
        request.session["user_id"] = user_id
        response_str = gt.get_response_str()
        return HttpResponse(response_str)
    
    # 极验验证登录
    def login_geetest(request):
        if request.method == "POST":
            msg = {}
            username = request.POST.get('username', None)
            password = request.POST.get('password', None)
            gt = GeetestLib(pc_geetest_id, pc_geetest_key)
            challenge = request.POST.get(gt.FN_CHALLENGE, '')
            validate = request.POST.get(gt.FN_VALIDATE, '')
            seccode = request.POST.get(gt.FN_SECCODE, '')
            status = request.session[gt.GT_STATUS_SESSION_KEY]
            user_id = request.session["user_id"]
            if status:
                result = gt.success_validate(challenge, validate, seccode, user_id)
            else:
                result = gt.failback_validate(challenge, validate, seccode)
            # 如果通过验证
            if result:
                user_obj = auth.authenticate(username=username, password=password)
                if user_obj:
                    auth.login(request, user_obj)
                else:
                    msg = {
                        'state': 1,  # 状态码
                        'error': '用户名或密码错误',  # 错误信息
                        'class': 'has-error'  # input框的样式类
                    }
                # return HttpResponse(msg)
                return JsonResponse(msg)
        return HttpResponse("error")
    views.py      
  • 相关阅读:
    [leetcode]5最长回文子串
    [leetcode]4寻找两个有序数组的中位数
    [leetcode]3无重复字符的最长字串
    [leetcode]2两数相加
    [leetcode]1两数之和
    [学习记录]堆
    [学习记录]平衡树
    [学习记录]二叉树删除
    [学习记录]排序算法总结
    创建mysql数据库
  • 原文地址:https://www.cnblogs.com/aizhinong/p/12261565.html
Copyright © 2020-2023  润新知