TariqBkn / recaptcha-v3-java-example

Java example of Google reCAPTCHA v3 integrated in Java web application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recaptcha V3 Java Example

Java example of Google reCAPTCHA v3 integration in a java web application

  • server side: GoogleRecaptcha.java for recaptcha verification
  • client side: see below

Client side part

Add a hidden input field to your web form

<input type="hidden" id="captchatoken" name="captchatoken" size="1" value=""/>

Include Google Recaptcha JavaScript libary

<script src="https://www.google.com/recaptcha/api.js?render=YOUR_PUBLIC_KEY"></script>

Insert small JavaScript that fill hidden recaptcha token input field

<script>
  grecaptcha.ready(function() {
  	grecaptcha.execute('YOUR_PUBLIC_KEY', {action: 'register'}).then(function(token) {
          $('#captchatoken').val(token)
  	});
  });
</script>

Server side verification

After setting up the client side part, now you can verify the recaptcha token from client side on the server. If the user is valid (human) it will return true, if the user is a bot false

GoogleRecaptcha.isValid(captchatoken)

PS: I'm using this in a Play Framework 1 Web Application

About

Java example of Google reCAPTCHA v3 integrated in Java web application

License:MIT License


Languages

Language:Java 100.0%