Add Google Verification To Public API

Posted By : Anil Kumar | 29-Nov-2017

Problem Identification: Sometimes we are required to create public API in our project for many purposes.

Here is the main problem is that someone may slow down your web project by sending too many requests.

To overcome this problem you can add google verification to your API.

Example: 

First, create a web page and add the google captcha to your web page to authenticate the user. 

<html>
   
Belfrics/Crypto Currency

<script> function recaptchaCallback() { UserAction(); }; function UserAction() { document.cookie = "username=test"; var xhttp = new XMLHttpRequest(); var url = window.location.href; xhttp.open("GET", url); xhttp.setRequestHeader("Content-type", "application/json"); xhttp.send(); xhttp.onreadystatechange = function() { if(this.readyState == 4){ if (this.status == 200) { document.getElementById("logo-hide").style.display = 'none'; document.getElementById("footer-hide").style.display = 'none'; document.write(xhttp.responseText); } } }; } </script> <script src='https://www.google.com/recaptcha/api.js'> </script>

Please click on captcha below

Please complete the security check to proceed.

 

</html>

Now add this web page in your API response. This web page redirects the result by calling the API again if google captcha is true.

 
And the API code is below
 
@ResponseBody
@RequestMapping(value = URLMapping.FETCH_MARKET_DATA, method = RequestMethod.GET)
Object fetchAllMarketData(HttpServletRequest request, String name) {
    name = "username";
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie: cookies) {
            if (cookie.getName().equals(name)) {
                Map < String, Object > result = new HashMap < String, Object > ();
                try {
                    result = orderBookReportService.fetchMarketData();
                    if ((Boolean) result.get("isSuccess")) {
                        return result;
                    }
                } catch (Exception e) {
                    return ResponseHandler.generateResponse(HttpStatus.INTERNAL_SERVER_ERROR, false, e.getMessage(),
                        result);
                }
            }
        }
    }
    ModelAndView mav = new ModelAndView("bel");
    return mav;
}
 

 

About Author

Author Image
Anil Kumar

Anil is a Web Developer who specializes in creating dynamic and beautiful web projects and has good experience of working in distributed teams.

Request for Proposal

Name is required

Comment is required

Sending message..