【问题标题】:Best way to use google recaptcha V3 in django在 django 中使用 google recaptcha V3 的最佳方式
【发布时间】:2019-07-25 08:31:37
【问题描述】:

首先我应该说我读过this,这不是我需要的。我想要使​​用 html/css 而不是 django 表单制作的表单。

而我的方法是基于this,其实就是PHP。

我编写了代码,它运行良好但我相信应该有更好的方法来做到这一点。

代码摘要是我提交表单,将数据和谷歌recaptcha令牌发布到我的函数,然后对其进行一些处理,然后根据处理结果重定向到相关页面,我返回url和状态再次到 jQuery,并使用 jQuery 重定向到该页面。

这是我的代码:

login.html:

<script src="https://www.google.com/recaptcha/api.js?render=here is recaptcha public key"></script>
<!-- login form and etc -->

<script>
$('#loginForm').submit(function() {
    // stop what loginform is going to do
    event.preventDefault();
    var username = $('#username').val();
    var password = $("#password").val();
    grecaptcha.ready(function () {
      grecaptcha.execute("here is recaptcha public key", 
         {action: "{% url 'basic_app:login_page' %}"}).then(function (token_id) 
              {$('#loginForm').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token_id + '">');
                $.post("{% url 'basic_app:login' %}",  // url
                   {username: username,password: password,
                    token_id: token_id,csrfmiddlewaretoken: '{{ csrf_token }}'},
                       function( result ){
                            console.log(result);
                            if(result.status == 0) {
                                    window.location.replace(result.url)
                            } else if (result.status == 1){
                                    window.location.replace(result.url)
                            }
                        },
                'json');
          });
      });
  });

views.py:

def user_login(request):
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        token_id = request.POST.get('token_id')
        if(token_id):
            secretKey = "here is recaptcha secret key"
            data = {
                'secret': secretKey,
                'response': token
            }
            r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
            result = r.json()
            ....
            ## some codes for more security 
            ....
            response = {'status': 0, 'message':"", 'url': '/'}
            return HttpResponse(json.dumps(response), content_type='application/json')
        else:
            response = {'status': 0, 'message':"", 'url': '/login_page'}
            return HttpResponse(json.dumps(response), content_type='application/json')     

....

这种方法有安全问题吗?
有什么方法可以编写更好的代码来使用 recaptcha V3 吗? 谢谢。

【问题讨论】:

    标签: python jquery django recaptcha


    【解决方案1】:

    更好的步骤:

    1. 当用户点击提交时,阻止默认行为
    2. 在 onSubmit 中,执行 g-recaptcha 并将令牌存储在表单中
    3. 调用 this.submit() 或 form.submit(),如下面的教程所示
    4. 在您的视图中验证验证码。

    How to Implement Google Recaptcha v3 on your django app

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 2022-01-19
      • 2020-02-16
      • 2019-05-03
      • 2012-09-07
      • 2022-07-16
      • 2020-12-22
      • 2021-05-16
      相关资源
      最近更新 更多