Google reCaptcha security validation using Angular, Spring boot

What you’ll build


in this article we'll build an application that uses google captcha using JAVA, Spring boot, Angular.


Google Captcha

CAPTCHA or Completely Automated Public Turing test to Tell Computers and Humans Apart is a technique to distinguish between humans and computers. CAPTCHA is mainly used as a security (spam, brute force, etc) check to ensure only human users can pass through. Generally, computers or bots are not capable of solving a captcha.

Google reCaptcha

In order to use google recaptcha you need to sign up for an api key pair for your websites (sign up for an API key pair).

Site key: is the public key that will be used in our frontend (Angular 6) app.

Secret key: is the private key that will be used in our backend app (Spring boot).

Spring boot


Maven dependencies


The json library is used to parse google captcha validation http response.

Java Code


import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.Map;

public class CaptchaRestController {
    @RequestMapping(value="verify/captcha", method = RequestMethod.POST)
    public ResponseEntity<?> validateReCaptha(@RequestBody Map<String, String> captchaMap){
        if(!isCaptchaValid("private-key", captchaMap.get("captcha"))){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        return ResponseEntity.noContent().build();

    public static boolean isCaptchaValid(String secretKey, String response) {
        try {
            String url = ""
                    + "secret=" + secretKey
                    + "&response=" + response;
            InputStream res = new URL(url).openStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(res, Charset.forName("UTF-8")));

            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = != -1) {
                sb.append((char) cp);
            String jsonText = sb.toString();

            JSONObject json = new JSONObject(jsonText);
            return json.getBoolean("success");
        } catch (Exception e) {
            return false;


Replace private-key text with your private key.


Generate the project

ng new captcha-angular --routing
npm install ng-recaptcha --save
npm install ngx-toastr --save
ng g c components/yami-code-captcha --spec false
ng g s services/captcha --spec false

ng-recaptcha is the angular library that we will use.[ng-recaptcha]

ngx-toastr: Used just for notifications (not mandatory).

Angular code

  <re-captcha (resolved)="resolved($event)" siteKey="public-key"></re-captcha>

<button style="color: #fff;background-color: #1D71B8;border-color: #1D71B8;" [disabled]="token=='' || !token" (click)="onSubmit()" type="button">
import { Component, OnInit } from '@angular/core';
import { CaptchaService } from 'src/app/services/captcha.service';
import { ToastrService } from 'ngx-toastr';

  selector: 'app-yami-code-captcha',
  templateUrl: './yami-code-captcha.component.html',
  styleUrls: ['./yami-code-captcha.component.css']
export class YamiCodeCaptchaComponent implements OnInit {
  token: String = '';
  constructor(private captchaService: CaptchaService, private toastr: ToastrService) { }

  ngOnInit() {

  onSubmit() {
        () => { this.toastr.success('form sent correctly'); },
        (error) => {
          this.toastr.error('invalid token');

  resolved(captchaResponse: string) {
    this.token = captchaResponse;
    console.log(`Resolved captcha with response ${captchaResponse}:`);

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment';

  providedIn: 'root'
export class CaptchaService {
  url: string = environment.url + 'verify/captcha';

  constructor(private http: HttpClient) { }

  post(captcha) {
    return, {captcha: captcha});
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { YamiCodeCaptchaComponent } from './components/yami-code-captcha/yami-code-captcha.component';
import { HttpClientModule } from '@angular/common/http';
import { ToastrModule } from 'ngx-toastr';
import { RecaptchaModule } from 'ng-recaptcha';

  declarations: [
  imports: [
    ToastrModule.forRoot({ timeOut: 3000 }),
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }
export const environment = {
  production: false,
  url: 'http://localhost:8080/'
export class AppModule { }


Source code:

The full implementation of this artcile can be found in the GitHub project. Download and unzip the source repository for this guide GIT, or clone it using Git: git clone