Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate google recaptcha in spring boot?

public class GoogleReCaptchaChecker {

        @Value("${google.recaptcha.url}")
        private String url;

        @Value("${google.recaptcha.secret-key}")
        private String secretKey;

        @Value("${google.recaptcha.site-key}")
        private String siteKey;

        @Autowired
        RestTemplate restTemplate;

        public Boolean validateGoogleReCaptcha(String reCaptchaResponseStr, String remoteAddr) {
            try {
                ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
                reCaptcha.setPrivateKey(secretKey);
                ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, siteKey, reCaptchaResponseStr);
                return reCaptchaResponse.isValid();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

        }

    }

This is my code for "google recaptcha" validation, but it always returns false, I have no idea about what are the arguments need to be passed. is there any maven dependency to do ReCaptcha validation?. I'm using this net.tanesha.recaptcha4j(version-0.0.7) dependency

like image 737
Jackson Baby Avatar asked Feb 14 '17 09:02

Jackson Baby


People also ask

How do I fix Google reCAPTCHA verification failed?

If you see the error again then please make sure that you first have Javascript enabled, and that your current browser does not have any extensions/add-ons enabled: Since ReCaptcha is enabled by Google services, using proxies or add-ons that block Google server requests will cause problems (an example being Ghostery)

What is a reCAPTCHA validation issue?

reCAPTCHA analyzes interactions with the website to detect if they are made by a human or some form of automated abuse. Sometimes, you may see a "failed reCAPTCHA check" error message while trying to create or amend your account. This means the website believes your actions may be those of a bot.

Why reCAPTCHA is not showing sometimes?

One of the most common reasons why this error occurs is that of an outdated Chrome version. reCAPTCHA will actively look at the browser version before allowing you access. This is applicable to all browser versions, not just Chrome. In this case, the solution is to update Google Chrome to the latest version.


1 Answers

There is a Spring Boot starter project witch will validate reCAPTCHA for you.

You just have to set the captcha validated URLs in your config file.

More on GitHub: https://github.com/Mr-DeWitt/spring-boot-recaptcha

like image 181
Szilárd Fodor Avatar answered Nov 10 '22 03:11

Szilárd Fodor