Implement reCAPTCHA v3 in Java / Spring


Even if I googled, there was no article for Java, and the official website was a little difficult to understand, so I summarized it.

Official link

-Google reCAPTCHA Official -About front-end implementation --About backend implementation

Reference link

-I introduced reCAPTCHA v3 to the inquiry form! --The basic implementation is concisely organized -I put in reCAPTCHA v3 --It is organized in consideration of error branching etc.

How to register

First, register to use the service.

I introduced reCAPTCHA v3 to the inquiry form! ⇒Refer to "How to register reCAPTCHA v3"

Implementation method

Once registered, all you have to do is implement it.

Client side

HTML Add the following in the form tag. Set the token obtained by JavaScript to this value and submit. If you use Ajax, you don't need this description by directly specifying token in Request parameter.


<input type="hidden" name="recaptchaResponse" id="recaptchaResponse">

Add the following to the script reading part. Make reCAPTCHA available on your site. Substitute the site key obtained at the time of registration for (site key).


<script src="'' +(Site key)"></script>

JavaScript Implement the function executed at form submit as follows. The value set in action ('contact_form' in the following) is stored in Response from reCAPTCHA, so it may be useful for analysis if you set where it was executed on the site.


$form = $(/*Get form*/);
$button = $(/*Get the submit button in the form*/);

$ {
		//For reCAPTCHA v3
		grecaptcha.ready(function () {
			grecaptcha.execute((Site key), {action: 'contact_form'}).then(function(token) {

				//The process described in the function before reCAPTCHA implementation is described here


Back end side

Controller Implement the POST destination method as follows. Actually, the logic part should be cut out to the Service layer. The BOT judgment (if statement part) will be explained in the next Model section.

@RequestMapping(value = "/hoge", method = RequestMethod.POST)
public String hogePost(
		@Valid @ModelAttribute("hogeForm") HogeForm hogeForm,
		BindingResult bindingResult,
		HttpServletRequest request, SitePreference sitePreference, Model model) {

	String url = "" +(Secret key)+ "&response=" + hogeForm.getRecaptchaResponse;
	RestTemplate restTemplate = new RestTemplate();
	RecaptchaResult result = restTemplate.getForObject(url, RecaptchaResult.class);"reCAPTCHA result: " + result.toString());

	if (result.isSuccess()) {
		if ( 0.5 <= result.getScore()) {
			//Describe the processing when it is not judged as BOT
		} else {
			//Describe the processing when it is judged as BOT
	} else {
		//Describe the processing when the connection fails to reCAPTCHA

Model Implement Model that stores Response of reCAPTCHA API as follows. I am using lombok which automatically generates getters and setters. Please refer to Official for the contents of Response.

Important thing

Note that the success property indicates the success / failure of the API connection, not the judgment of whether it is a BOT or not. In reCAPTCHA, the program determines whether it is a BOT based on the returned score property (0.0 to 1.0). The closer the score is to 1, the lower the possibility of BOT, and the closer it is to 0, the higher the possibility of BOT. Google Official Interpreting the score says that the initial threshold should be 0.5.

package com.croooober.v1.cr_www.model.api;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

@JsonIgnoreProperties(ignoreUnknown = true)
public class RecaptchaResult {
    private boolean success;
    private String challenge_ts;
    private String hostname;
    private float score;
    private String action;

    public RecaptchaResult() {

Also added properties to the Form object.

private String recaptchaResponse;

If you can implement it well, you can check the result with log output as follows.

reCAPTCHA result: RecaptchaResult(success=true, challenge_ts=2020-01-08T04:19:27Z,, score=0.9, action=contact_form)


At first, I thought that if I throw it in the API, it will judge whether it is a BOT or not. Actually, I had to judge by my own program based on score, which was a little confusing.

Recommended Posts

Implement reCAPTCHA v3 in Java / Spring
Implement two-step verification in Java
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
Implement Table Driven Test in Java 14
3 Implement a simple interpreter in Java
Implement PHP implode function in Java
Implement REST API in Spring Boot
Implement Spring Boot application in Gradle
Spring Java
Try to implement Yubaba in Java
1 Implement simple lexical analysis in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Try to implement n-ary addition in Java
Create Java Spring Boot project in IntelliJ
How to implement coding conventions in Java
Implement something like a stack in Java
[* Java *] I participated in JJUG CCC 2019 Spring
[Spring MVC] Implement dynamic parameters included in URL without using Optional (~ Java7)
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
[Java] Spring DI ③
FizzBuzz in Java
I tried to implement deep learning in Java
Java tips-Create a Spring Boot project in Gradle
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Inject Logger in Spring
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
I tried to implement Firebase push notification in Java
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Use Interceptor in Spring
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Quickly implement a singleton with an enum in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Microservices in Spring Cloud