OpenSource For You

Implementi­ng two-step verificati­on

-

Here’s a procedure to create a sample authentica­tion form in NetBeans and modify it to implement the two-step YHULfiFDWL­on IunFWLonDO­LWy: 1. Launch NetBeans, select Files > New Project or press

Ctrl+Shift+N. 2. In the New Project window, select Java under Categories,

Java Applicatio­n under Projects and click Next. 3. In the subsequent window, enter the Project Name as Two Step Ver to the Project Location, add TSV at the end (this will be the project folder created at the selected location), uncheck the Create Main Class check-box and click Finish. 6HH )LJuUH 1 IoU UHIHUHnFH. ThH nHw pUoMHFW Ls shown Ln WhH Projects panel at the left of the main window. 4. 1HxW, FUHDWH D bODnN GU, IoUP IoU DuWhHnWLFD­WLon. 6HOHFW Files > New File or press Ctrl+N. In the New File window, select Swing GUI Forms under Categories JFrame Forms under File Types and click Next. 5. In the New JFrame Form window, enter the Class Name as Authentica­tionForm, the Package as tsv, and click Finish. Observe that File Authentica­tionForm.java is added to the project under the tsv package seen in the Projects panel. Also, a blank canvas now appears in the Workspace (Figure 2). We’ll be using this to place the form elements. 6. 2n youU FDnYDs, DGG GLIIHUHnW IoUP HOHPHnWs—IouU oI them (3 JLabels, 1 JTextField, 1 JPasswordF­ield and 1 Jbutton) from Swing Controls in the palette on the right

and hDnG sLGH oI WhH PDLn wLnGow. CUHDWH D bDsLF ODyouW IoU the Authentica­tion Form as shown in Figure 3. Don’t worry about the text; you can format it in the next stage. However, you may change the size of the components to make the form look elegant. 7. To format the form, you need to set the text property oI HDFh FoPponHnW. COLFN D FoPponHnW, DnG Ln WhH Properties window (at the right of the main window) set WhH WHxW pUopHUWy. 6HH )LJuUH 4 IoU Dn HxDPpOH. 6LPLODUOy, set the text properties of the remaining components. Leave the text property blank for the JTextField and JPasswordF­ield. 6HH )LJuUH 5 IoU UHIHUHnFH. 8. vour form is now ready to use, but it’s still ‘deaf’. In order to make it listen, you need to add an event listener for the Submit button. When a user clicks it, the OLsWHnHU wLOO fiUsW YDOLGDWH WhH IoUP IoU HPpWy fiHOGs, WhHn extract the text from the JTextField and JPasswordF­ield and verify it against stored values (for this tutorial, hard-coded in the code itself). If successful, it sends a randomly generated 3-5 digit code to the users’ mobile phone and asks them to enter the same code. If the code entered matches the randomly generated code, the users hDYH pDssHG WhH Wwo-sWHp YHULfiFDWL­on. To DGG Dn HYHnW, right-click the Submit button and select Events > Action > actionPerf­ormed. This listener will listen to events triggered via the mouse as well as the keyboard for clicking the Submit button. It will create a method for the event listener, where you’ll place your event-handling code. It will also switch to Source view; to go back to the Design view, click Design. 9. In the event method, add the following lines of code. This FoGH hDnGOHs YDOLGDWLon, WhH fiUsW sWHp oI YHULfiFDWL­on, DnG LW JHnHUDWHs D UDnGoP fiYH-GLJLW FoGH WhDW’OO bH sHnW YLD HPDLO-Wo-606 Wo WhH UHJLsWHUHG PobLOH phonH nuPbHU. L*aeclaring the container variables*L ptring usrkame, usrmasswor­d; /*(xWUDFW UVeUnDPe DnG 3DVVwRUG IURP fieOGV*/ usrkame = jTextcield­1.getText(); usrmasswor­d = jmasswordc­ield1.getText();

/*VDOLGDWLnJ DJDLnVW ePSWy fieOGV*/ if(usrkame.equals(“”))javax.swing.glptionman­e. VhRw0eVVDJ­eDLDORJ(nuOO, “3OeDVe fiOO Whe UVeUnDPe )LeOG”); else if(usrmasswor­d.equals(“”))javax.swing.glptionman­e. VhRw0eVVDJ­eDLDORJ(nuOO, “3OeDVe fiOO Whe 3DVVwRUG )LeOG”);

/*,PSOePenWLn­J fiUVW VWeS RI veULfiFDWL­Rn*/ else { if(usrkame.equalsfgno­reCase(“pample rser”) && usrmasswor­d. equals(“5amp73”)) { MDvDx.VwLnJ.J2SWLRn3Dn­e.VhRw0eVVDJ­eDLDORJ(nuOO,”VeULfieG DW 6WeS 1”); /*,PSOePenWLn­J VeFRnG VWeS RI veULfiFDWL­Rn*/ L*denerating random code of 5 digits*L

 ??  ?? Figure 4: The Properties panel shown on right side of main window
Figure 4: The Properties panel shown on right side of main window

Newspapers in English

Newspapers from India