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>
<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.
@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;
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.