zoukankan      html  css  js  c++  java
  • django验证码

    1.验证准备:

    ①行为验证--->部署文档--->python--->手动下载压缩包文件

    ②打开示例中views视图,里面需要到官网注册的id

    pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c"
    pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4"
    

    ③安装依赖包 :pip install geetest

    ④输入用户名、密码后,点击登录按钮显示滑动验证码

    2.详细代码:

    ①前端代码

    1.静态文件包:
    <script src="/static/jquery-3.3.1.js"></script>
    <script src="/static/bootstrap/js/bootstrap.min.js"></script>
    2.及验科技提供的包:
    <!-- 引入封装了failback的接口--initGeetest -->
    <script src="http://static.geetest.com/static/tools/gt.js"></script>
    3.登录展示页面:
    <form  method="post" action="">
        {% csrf_token %}
        用户名:<input  type="text"  name="username" id="username"/></br>
        密码:<input  type="password"  name="password" id="password"/></br>
        <input  type="button" id="login-button"/>
     </form>
    <div class="form-group">
                    <!-- 放置极验的滑动验证码 -->
                    <div id="popup-captcha"></div>
    </div>
    <span class="login-error"></span>
    4.固定写法:
    <script>
    
        // 极验 发送登录数据的
        var handlerPopup = function (captchaObj) {
            // 成功的回调
            captchaObj.onSuccess(function () {
                var validate = captchaObj.getValidate();
                // 1. 取到用户填写的用户名和密码 -> 取input框的值
                var username = $("#username").val();
                var password = $("#password").val();
                $.ajax({
                    url: "/login/", // 进行二次验证
                    type: "post",
                    dataType: "json",
                    data: {
                        username: username,
                        password: password,
                        csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
                        geetest_challenge: validate.geetest_challenge,
                        geetest_validate: validate.geetest_validate,
                        geetest_seccode: validate.geetest_seccode
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.status) {                  //此处可以自定义修改
                            // 有错误,在页面上提示
                            $(".login-error").text(data.msg);
                        } else {
                            // 登陆成功
                            location.href = data.msg;
                        }
                    }
                });
            });
    
             $("#login-button").click(function () {
                captchaObj.show();
            });
            // 将验证码加到id为captcha的元素里
            captchaObj.appendTo("#popup-captcha");
            // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
        };
        // 当input框获取焦点时将之前的错误清空
        $("#username,#password").focus(function () {
            // 将之前的错误清空
            $(".login-error").text("");
        });
    
        // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback)
        $.ajax({
            url: "/pc-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 // 表示用户后台检测极验服务器是否宕机,一般不需要关注
                    // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
                }, handlerPopup);
            }
        })
    
    
    </script>
    

    ②后端代码

    from django.shortcuts import render,HttpResponse
    # Create your views here.
    from geetest import GeetestLib
    from django.contrib import auth
    from django.http import JsonResponse
    
    def index(request):
    
        return render(request,'index.html')
    
    def login(request):
        # if request.is_ajax():  # 如果是AJAX请求
        if request.method == "POST":
            # 初始化一个给AJAX返回的数据
            ret = {"status": 0, "msg": ""}
            # 从提交过来的数据中 取到用户名和密码
            username = request.POST.get("username")
            pwd = request.POST.get("password")
            # 获取极验 滑动验证码相关的参数
            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:
                # 验证码正确
                # 利用auth模块做用户名和密码的校验
                user = auth.authenticate(username=username, password=pwd)
                if user:
                    # 用户名密码正确
                    # 给用户做登录
                    auth.login(request, user)
                    ret["msg"] = "/index/"
                else:
                    # 用户名密码错误
                    ret["status"] = 1
                    ret["msg"] = "用户名或密码错误!"
            else:
                ret["status"] = 1
                ret["msg"] = "验证码错误"
    
            return JsonResponse(ret)
        return render(request, "register.html")
    
    
    # 请在官网申请ID使用,示例ID不可使用
    pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c"
    pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4"
    
    # 处理极验 获取验证码的视图
    def get_geetest(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)
    

    3.form表单提交中的按钮是普通按钮,不是submit!!!!!!!

  • 相关阅读:
    codeblocks c++11 pthread
    腾讯笔试题目
    Unity3D协程介绍 以及 使用
    Unity手游之路<六>游戏摇杆之Easy Touch 3教程
    Unity3d 换装 之 模型动画分离
    Starling 2D框架简介(一)
    [译林军] 译~CrossBridge 简介
    Flash, Flex, Air, Flashplayer之间的相互关系是什么?
    网络和通信基础知识
    页游安全攻与防,SWF加密和隐藏密匙
  • 原文地址:https://www.cnblogs.com/zhuxianxiaofan/p/13177439.html
Copyright © 2011-2022 走看看