Implementation of 2 factor for OTP verification
Posted By : Sudhakar Pandey | 22-Sep-2017
Go to register on https://2factor.in/CP/register.php for start OTP messaging for verification or you can use it for send bulk send messages
After successfully register go to login and create authetication token for using this on your code and make this entry in your application properties.
ex- apiKey.dev=e20a51a6-82c8-11e7-94da-0200cd936042
Go to register on https://2factor.in/CP/register.php for start OTP messaging for verification or you can use it for send bulk send messages
After successfully register go to log in and create authentication token for using this on your code and make this entry in your application properties.
ex- apiKey.dev=e20a51a6-82c8-11e7-94da-0200cd936042
If you want to use send otp messages then first you need to create an otp templates by link https://2factor.in/CP/2FA_SMS_OTP_Templates_list.php and you need to click on 'Create new OTP Template' after that you see page like below-
There is a field you need to fill it your value.
Template Name - This is like your template identifier name
Template - In this field,
OTP template must be less than 150 characters and must contain string XXXX ( 4 'X' characters ),
which will be replaced with actual OTP value.
Ex.
To send message
'1234 is your one-time password for 2Factor.in' OR
'123456 is your one-time password for 2Factor.in'
OTP template must be defined as
'XXXX is your one-time password for 2Factor.in'
and some additional details need to be filled as
Company Name and Website.
After you need to be integrate 2factor API for it-
https://2factor.in/API/V1/"apiKey"/SMS/phoneCode/mobNum/otp/templateName;
Below code explains how to implement API in Java code-
public MapsendOtpWithTwoFactor(Long mobNum,String phoneCode,String otp){ LOGGER.info("Enter into send OTP method for indian number..."); Map result = new HashMap (); RestTemplate restTemp = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); HttpEntity entity = new HttpEntity ("parameters", headers); String apiKey = envConfiguration.getTwoFactorApiKey(); String url="https://2factor.in/API/V1/"+apiKey+"/SMS/+"+phoneCode+mobNum+"/"+otp+"/"+"BELICO"; LOGGER.info("URL:::::::::"+url); ResponseEntity response = null; response = restTemp.exchange(url, HttpMethod.GET, entity, String.class); LOGGER.info("Response :::::::"+response.getBody()); JSONObject txnObject = new JSONObject(response.getBody()); LOGGER.info("txnObject"+txnObject); if(txnObject.get("Status").equals("Success")){ LOGGER.info("OTP send successfully"); result.put("otpResponse", txnObject); return result; } else { LOGGER.info("OTP not send"); result.put("otpResponse", txnObject); return result; } }
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
Sudhakar Pandey
Sudhakar pandey is java developer and passoinate about learning to new technology, He have exposure about spring, hibernate, REST services, Multithreading , JSP/ Servlet. On the database side He have exposure about MySql, Oracle, Postgres.