reCAPTCHA V3:空闲后如何处理过期令牌?

reCAPTCHA V3: how to deal with expired token after idle?

对于 Google reCAPTCHA V2,很清楚当令牌因空闲而过期时要做什么:客户更改为再次单击 reCaptcha 复选框。 对于 Google reCAPTCHA V3,它是不同的,因为不清楚令牌何时因空闲而过期。

对于 reCAPTCHA V3,Google 建议:

https://developers.google.com/recaptcha/docs/v3

  1. 使用您的站点密钥加载 JavaScript api

  2. 在某个操作上调用 grecaptcha.execute 或在页面加载时调用 // 我们选择页面加载时,好吗?

  3. 将令牌发送到您的后端请求验证//点击按钮

好的。如果在页面加载几分钟后单击按钮,则我们发送到后端的 V3 令牌已经过期。 在这种情况下,正确的处理方式是什么?我们是否应该通过每分钟向 Google 发送调用来自动更新令牌? 这种情况下最好的方法是什么?我没有找到 Google.

的任何建议

令牌必须晚于页面加载生成。它应该在我们将它发送到后端之前生成。换句话说,我们点击按钮(来自我的示例),然后获取生成的令牌,然后将令牌发送到后端。

这个解决方案很有意义并解决了我的问题。

我以 asp.net 形式工作,但此解决方案适用于任何语言。令牌过期的问题很烦人

令牌在 v3 中的有效期为 2 分钟,但 google 不推荐保留定时器每 2 分钟刷新一次令牌的做法。他们建议仅在需要时刷新令牌。

我选择了 javascript 解决方案,强制客户端单击刷新令牌的按钮。

需要注意的是,如果在刷新recaptcha时执行“recaptcha.ready”,则会抛出错误,所以我不得不将“ready”和“execute”分开,这样recaptcha就是刷新无误。

<script type="text/javascript" >
    grecaptcha.ready(function () {
      captcha_execute();
    });

    function captcha_execute() {
      grecaptcha.execute('<%=System.Configuration.ConfigurationManager.AppSettings("recaptcha-public-key").ToString %>', { action: 'ingreso_usuario_ext' }).then(function (token) {
        document.getElementById("g-recaptcha-response").value = token;
      });
    }

    function los_dos(token_viejo) {
      captcha_execute()
      clase_boton(token_viejo);
    }

    async function clase_boton(token_viejo) {
      btn_act = document.getElementById("Btn_Refrescar");
      btn = document.getElementById("Btn_Ingresar");
      btn.setAttribute("class", "button_gris");
      btn_act.style.display = "none";
      btn.style.display = "initial";
      btn.disabled = true;

      //token_viejo = document.getElementById("g-recaptcha-response").value;
      strToken = token_viejo;
      varCant = 0;

      while (strToken == token_viejo && varCant < 30) {
        strToken = document.getElementById("g-recaptcha-response").value;
        await sleep(100);
        varCant++;
      }

      btn.setAttribute("class", "button_azul");
      btn.disabled = false;

      setTimeout(refrescar_token, 120000);
    }

    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }

    function refrescar_token() {
      btn_ing = document.getElementById("Btn_Ingresar");
      btn_act = document.getElementById("Btn_Refrescar");
      btn_act.style.display = "initial";
      btn_ing.style.display = "none";
    }
  </script>

体内

<body style="background-color: #dededc;" onload="clase_boton('');" >

按钮

 <asp:Button ID="Btn_Ingresar" runat="server" Text="Ingresar" CssClass="button_gris" Enabled="false" />
 <input type="button" id="Btn_Refrescar" name="Btn_Refrescar" class="button_verde" value="Refrescar Token" title="Refrescar Token" onclick="los_dos(document.getElementById('g-recaptcha-response').value);" style="display: none;" />
          

使用 javascript,我等待令牌被填充,当它被填充时,我启用登录按钮。如果这个过程花费的时间太长(由于某些错误),我仍然会启用它。这是一个选择的问题。

2 分钟后(“setTimeout”),登录按钮变得不可见,我显示按钮以刷新令牌。

希望helps/guides能解决您的问题。

由于 reCAPTCHA 令牌会在两分钟后过期,因此我是这样工作的:

第 1 步:在页面加载时加载验证码令牌(像往常一样)

第 2 步:使用 SetInterval 函数每 90 秒重新加载一次令牌,以便在 2 分钟后过期之前刷新 reCAPTCHA 令牌。

// Onload
grecaptcha.ready(function () {
  grecaptcha.execute('YOUR_KEY_HERE', { action: 'request_call_back' }).then(function (e) {
    $('#YOUR_FIELD_NAME_ID').val(e);
  });
});

// Every 90 Seconds
setInterval(function () {
  grecaptcha.ready(function () {
    grecaptcha.execute('YOUR_KEY_HERE', { action: 'request_call_back' }).then(function (e) {
      $('#YOUR_FIELD_NAME_ID').val(e);
    });
  });
}, 90 * 1000);