Use of extreme validation (nodejs deployment)

Keywords: Session SDK JSON supervisor

Add some verification codes to the page to avoid malicious external damage.The following is a summary of my work validating deployments with nodejs pairs.

1. Verify ideas with nodejs deployment poles:
Installation dependencies: request, express, body-parser, supervisor, express-session.
Install the SDK using npm install gt3-sdk-save, then introduce: var Geetest = require('gt3-sdk');
Deploy the first interface:

var click = require('./static/click');
app.get("/gt/register-click", function (req, res) {

    // Request challenge for each validation from a polar test
    click.register(null, function (err, data) {
        if (err) {
            console.error(err);
            res.status(500);
            res.send(err);
            return;
        }

        if (!data.success) {
            // Enter failback, if you've been in this mode, check that the server is accessible to the polar server
            apirefer

            // In case, you can choose one of the following two ways:

            // 1. Continue to use the failback alternative provided by the polar test
            req.session.fallback = true;
            res.send(data);

            // 2. Use your own alternatives
            // todo
        } else {
            // Normal mode
            req.session.fallback = false;
            res.send(data);
        }
    });
});

Deploy the second interface:

app.post("/gt/validate-click", function (req, res) {
    // Secondary validation of the validation credentials provided by ajax
    click.validate(req.session.fallback, {
        geetest_challenge: req.body.geetest_challenge,
        geetest_validate: req.body.geetest_validate,
        geetest_seccode: req.body.geetest_seccode
    }, function (err, success) {
        if (err) {
            // network error
            res.send({
                status: "error",
                info: err
            });

        } else if (!success) {
            // Secondary validation failed
            res.send({
                status: "fail",
                info: 'Logon Failure'
            });
        } else {
            res.send({
                status: "success",
                info: 'Login Successful'
            });
        }
    });
});

2. Very effective use of web pages:
Initialized JS file introduced: <script src="gt.js"></script>
Initialize by calling an initialization function:

$.ajax({
        url: "gt/register-click?t=" + (new Date()).getTime(), // Add random numbers to prevent caching
        type: "get",
        dataType: "json",
        success: function (data) {
            // Initialize by calling initGeetest
            // Parameter 1: Configuration parameters
            // Parameter 2: Callback, the first parameter validation code object of the callback, which can then be used to call the corresponding interface
            initGeetest({
                // The following four configuration parameters are required and cannot be missing
                gt: data.gt,
                challenge: data.challenge,
                offline: !data.success, // Indicates whether the user's background check server is down
                new_captcha: data.new_captcha, // Downtime when used for downtime indicates a new verification code

                product: "popup", // Product forms, including: float, popup
                width: "300px"
                // See: http://docs.geetest.com/install/client/web-front/
            }, handler);
        }
    });

After the initialization is complete, the specific process operation is carried out.

var handler = function (captchaObj) {
        captchaObj.appendTo('#captcha');
        captchaObj.onReady(function () {
            $("#wait").hide();
        });
        $('#btn').click(function () {
            var result = captchaObj.getValidate();
            if (!result) {
                return alert('Please complete verification');
            }
            $.ajax({
                url: 'gt/validate-click',
                type: 'POST',
                dataType: 'json',
                data: {
                    username: $('#username2').val(),
                    password: $('#password2').val(),
                    geetest_challenge: result.geetest_challenge,
                    geetest_validate: result.geetest_validate,
                    geetest_seccode: result.geetest_seccode
                },
                success: function (data) {
                    if (data.status === 'success') {
                        alert('Login Successful');
                    } else if (data.status === 'fail') {
                        alert('Login failed, please complete verification');
                        captchaObj.reset();
                    }
                }
            });
        })
        // See: http://docs.geetest.com/install/client/web-front/
    };

You can also switch to the bind button login mode by replacing the parameter product in the initialization function with: bind.
Subsequent code changes are as follows:

var handler = function (captchaObj) {
        captchaObj.appendTo('#captcha');
        captchaObj.onReady(function () {
            $("#wait").hide();
        });
        captchaObj.onSuccess(function(){
            var result = captchaObj.getValidate();
            if (!result) {
                return alert('Please complete verification');
            }
            $.ajax({
                url: 'gt/validate-click',
                type: 'POST',
                dataType: 'json',
                data: {
                    username: $('#username2').val(),
                    password: $('#password2').val(),
                    geetest_challenge: result.geetest_challenge,
                    geetest_validate: result.geetest_validate,
                    geetest_seccode: result.geetest_seccode
                },
                success: function (data) {
                    if (data.status === 'success') {
                        alert('Login Successful');
                    } else if (data.status === 'fail') {
                        alert('Login failed, please complete verification');
                        captchaObj.reset();
                    }
                }
            });
            // See: http://docs.geetest.com/install/client/web-front/
        })
        window.gt = captchaObj;
    };

    $('#btn').click(function(){
        window.gt.verify();
    })

The final specific project code can be referred to as: https://github.com/wwjhzc/A-validation
Project Deployment Official Documentation: https://docs.geetest.com/install/deploy/client/web
Author: rookie.he (kuke_kuke)
Blog links: http://blog.csdn.net/qq_33599109
Welcome to support, thank you!

Posted by djdog.us on Tue, 24 Mar 2020 09:35:48 -0700