Registration with Spring – Integrate reCAPTCHA

1. Overview

In this article we’ll continue the Spring Security Registration series by adding Google reCAPTCHA to the registration process in order to differentiate human from bots.

2. Integrating Google’s reCAPTCHA

To integrate Google’s reCAPTCHA web-service, we first need to register our site with the service, add their library to our page, and then verify the user’s captcha response with the web-service.

Let’s register our site at The registration process generates a site-key and secret-key for accessing the web-service.

2.1. Storing the API Key-Pair

We store the keys in the

And expose them to Spring using a bean annotated with @ConfigurationProperties:

@ConfigurationProperties(prefix = "google.recaptcha.key")
public class CaptchaSettings {

    private String site;
    private String secret;

    // standard getters and setters

2.2. Displaying the Widget

Building upon the tutorial from series, we’ll now modify the registration.html to include Google’s library.

Inside our registration form, we add the reCAPTCHA widget which expects the attribute data-sitekey to contain the site-key.

The widget will append the request parameter g-recaptcha-response when submitted:

<!DOCTYPE html>


<script src=''></script>


    <form action="/" method="POST" enctype="utf8">

        <div class="g-recaptcha col-sm-5"
        <span id="captchaError" class="alert alert-danger col-sm-4"

3. Server Side Validation

The new request parameter encodes our site key and a unique string identifying the user’s successful completion of the challenge.

However, since we cannot discern that ourselves, we cannot trust what the user has submitted is legitimate. A server-side request is made to validate the captcha response with the web-service API.

The endpoint accepts a HTTP request on the URL, with the query parameters secret, response, and remoteip. It returns a json response having the schema:

    "success": true|false,
    "challenge_ts": timestamp,
    "hostname": string,
    "error-codes": [ ... ]

3.1. Retrieve User’s Response

The user’s response to the reCAPTCHA challenge is retrieved from the request parameter g-recaptcha-response using HttpServletRequest and validated with our CaptchaService. Any exception thrown while processing the response will abort the rest of the registration logic:

public class RegistrationController {

    private ICaptchaService captchaService;


    @RequestMapping(value = "/user/registration", method = RequestMethod.POST)
    public GenericResponse registerUserAccount(@Valid UserDto accountDto, HttpServletRequest request) {
        String response = request.getParameter("g-recaptcha-response");

        // Rest of implementation


3.2. Validation Service

The captcha response obtained should be sanitized first. A simple regular expression is used.

If the response looks legitimate, we then make a request to the web-service with the secret-key, the captcha response, and the client’s IP address:

public class CaptchaService implements ICaptchaService {

    private CaptchaSettings captchaSettings;

    private RestOperations restTemplate;

    private static Pattern RESPONSE_PATTERN = Pattern.compile("[A-Za-z0-9_-]+");

    public void processResponse(String response) {
        if(!responseSanityCheck(response)) {
            throw new InvalidReCaptchaException("Response contains invalid characters");

        URI verifyUri = URI.create(String.format(
          getReCaptchaSecret(), response, getClientIP()));

        GoogleResponse googleResponse = restTemplate.getForObject(verifyUri, GoogleResponse.class);

        if(!googleResponse.isSuccess()) {
            throw new ReCaptchaInvalidException("reCaptcha was not successfully validated");

    private boolean responseSanityCheck(String response) {
        return StringUtils.hasLength(response) && RESPONSE_PATTERN.matcher(response).matches();

3.3. Objectifying the Validation

A Java-bean decorated with Jackson annotations encapsulates the validation response:

@JsonIgnoreProperties(ignoreUnknown = true)
public class GoogleResponse {

    private boolean success;

    private String challengeTs;

    private String hostname;

    private ErrorCode[] errorCodes;

    public boolean hasClientError() {
        ErrorCode[] errors = getErrorCodes();
        if(errors == null) {
            return false;
        for(ErrorCode error : errors) {
            switch(error) {
                case InvalidResponse:
                case MissingResponse:
                    return true;
        return false;

    static enum ErrorCode {
        MissingSecret,     InvalidSecret,
        MissingResponse,   InvalidResponse;

        private static Map<String, ErrorCode> errorsMap = new HashMap<String, ErrorCode>(4);

        static {
            errorsMap.put("missing-input-secret",   MissingSecret);
            errorsMap.put("invalid-input-secret",   InvalidSecret);
            errorsMap.put("missing-input-response", MissingResponse);
            errorsMap.put("invalid-input-response", InvalidResponse);

        public static ErrorCode forValue(String value) {
            return errorsMap.get(value.toLowerCase());

    // standard getters and setters

As implied, a truth value in the success property means the user has been validated. Otherwise the errorCodes property will populate with the reason.

The hostname refers to the server that redirected the user to the reCAPTCHA. If you manage many domains and wish them all to share the same key-pair, you can choose to verify the hostname property yourself.

3.4. Validation Failure

In the event of a validation failure, an exception is thrown. The reCAPTCHA library needs to instruct the client to create a new challenge.

We do so in the client’s registration error-handler, by invoking reset on the library’s grecaptcha widget:


    var formData= $('form').serialize();
    $.post(serverContext + "user/registration", formData, function(data){
        if(data.message == "success") {
            // success handler
    .fail(function(data) {

        if(data.responseJSON.error == "InvalidReCaptcha"){

4. Protecting Server Resources

Malicious clients do not need to obey the rules of the browser sandbox. So our security mindset should be at the resources exposed and how they might be abused.

4.1. Attempts Cache

It is important to understand that by integrating reCAPTCHA, every request made will cause the server to create a socket to validate the request.

While a more layered approach would be needed for a true DoS mitigation; We can implement an elementary cache that restricts a client to 4 failed captcha responses:

public class ReCaptchaAttemptService {
    private int MAX_ATTEMPT = 4;
    private LoadingCache<String, Integer> attemptsCache;

    public ReCaptchaAttemptService() {
        attemptsCache = CacheBuilder.newBuilder()
          .expireAfterWrite(4, TimeUnit.HOURS).build(new CacheLoader<String, Integer>() {
            public Integer load(String key) {
                return 0;

    public void reCaptchaSucceeded(String key) {

    public void reCaptchaFailed(String key) {
        int attempts = attemptsCache.getUnchecked(key);
        attemptsCache.put(key, attempts);

    public boolean isBlocked(String key) {
        return attemptsCache.getUnchecked(key) >= MAX_ATTEMPT;

4.2. Refactoring the Validation Service

The cache is incorporated first by aborting if the client has exceeded the attempt limit. Otherwise when processing an unsuccessful GoogleResponse we record the attempts containing an error with the client’s response. Successful validation clears the attempts cache:

public class CaptchaService implements ICaptchaService {

    private ReCaptchaAttemptService reCaptchaAttemptService;


    public void processResponse(String response) {


        if(reCaptchaAttemptService.isBlocked(getClientIP())) {
            throw new InvalidReCaptchaException("Client exceeded maximum number of failed attempts");


        GoogleResponse googleResponse = ...

        if(!googleResponse.isSuccess()) {
            if(googleResponse.hasClientError()) {
            throw new ReCaptchaInvalidException("reCaptcha was not successfully validated");

5. Conclusion

In this article we integrated Google’s reCAPTCHA library into our registration page and implemented a service to verify the captcha response with a server-side request.

The full implementation of this tutorial is available in the github project – this is an Maven based project, so it should be easy to import and run as it is.

Leave a Reply

Your email address will not be published. Required fields are marked *