如何知道reCAPTCHA v3是否有效?

时间:2019-11-17 09:03:13

标签: node.js express vue.js vuetify.js captcha

我的前端像这样使用vuetify:

   validate: async function () {
    let tokenCaptcha
    await this.$recaptcha('login').then((token) => {
      tokenCaptcha = token
    })

    if (this.$refs.form.validate() && tokenCaptcha) {
        let params = {
          birthDate: this.birthday,
          emailAddress: this.email,
          name: this.fullname,
          phoneNumber: this.phone,
          recaptcha: tokenCaptcha
        }
        this.modalSummary = true
        await this.setSummary(params) // Async vuex store / post to api backend
        if (this.dataSummary) {
          this.success = true
        } else {
          this.success = false
        }
    }
  }

参考:https://www.npmjs.com/package/vue-recaptcha-v3

我的后端/服务器端使用像这样的快速js(节点js):

app.post('/summary', function (req, res) {
    if (req.body.recaptcha === undefined || req.body.recaptcha === '' || req.body.recaptcha === null) {
        res.send({success: false, msg: 'Please select captcha first'});
        return;
    }
    const secretKey = 'xxxxxx';
    const verificationURL = `https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${req.body.recaptcha}&remoteip=${req.connection.remoteAddress}`;
    https.get(verificationURL, (resG) => {
        let rawData = '';
        resG.on('data', (chunk) => { rawData += chunk })
        resG.on('end', function() {
            try {
                var parsedData = JSON.parse(rawData);
                if (parsedData.success === true) {
                    let data = { 
                        date_of_birth: req.body.birthDate,
                        email_address: req.body.emailAddress,
                        full_name: req.body.name,
                        phone_number: req.body.phoneNumber,
                    }
                    let sql = "INSERT INTO summary SET ?";
                    let query = conn.query(sql, data,(err, results) => {
                        if(err) throw err;
                        res.send({success: "OK", message: 'Success', data: data});
                    });
                } else {
                    res.send({success: false, msg: 'Failed captcha verification'});
                    return;
                }
            } catch (e) {
                res.send({success: false, msg: 'Failed captcha verification from Google'});
                return;
            }
        });
    });
});

该代码有效。但是如何知道reCAPTCHA v3是否有效?

因为版本3验证码没有出现。我只想确定我的代码流程是否正确。除此之外,我想检查验证码是否是机器人

3 个答案:

答案 0 :(得分:1)

  1. 在用户界面(右下),您应该具有以下功能:

enter image description here

  1. 在后端:获取Recaptcha响应的结果。像:
console.info(parsedData)

必须看起来像:

{
  "success": true|false,      // whether this request was a valid reCAPTCHA token for your site
  "score": number             // the score for this request (0.0 - 1.0)
  "action": string            // the action name for this request (important to verify)
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

^ https://developers.google.com/recaptcha/docs/v3#site_verify_response

答案 1 :(得分:0)

您也可以将令牌登录到控制台。 每次发送表单时,都会生成不同的令牌-通过这种方式,您知道验证码v3可以正常工作。

只需输入: console.log(token);

Yuu应该在控制台中获得这种类型的唯一字符串: enter image description here

答案 2 :(得分:0)

  1. 在网页(右下角)上,您应该有如下所示的 Google Captcha 图标:enter image description here

  2. 您可以在管理页面 (https://www.google.com/recaptcha/about/) 中验证点击次数和分数分布

enter image description here

相关问题