Question: I need to add a security feature to a Javafx GUi login application program where a file JavaMail will be read into the program. The

I need to add a security feature to a Javafx GUi login application program where a file "JavaMail" will be read into the program. The file will send an authentication email with a code to enforce multi-factor authenticatication. Java mail code and the Login application code is below.

login Application

package sdev425_2;

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage;

/** * * @author jim Adopted from Oracle's Login Tutorial Application * https://docs.oracle.com/javafx/2/get_started/form.htm */ public class SDEV425_2 extends Application {

@Override public void start(Stage primaryStage) { primaryStage.setTitle("SDEV425 Login"); // Grid Pane divides your window into grids GridPane grid = new GridPane(); // Align to Center // Note Position is geometric object for alignment grid.setAlignment(Pos.CENTER); // Set gap between the components // Larger numbers mean bigger spaces grid.setHgap(10); grid.setVgap(10);

// Create some text to place in the scene Text scenetitle = new Text("Welcome. Login to continue."); // Add text to grid 0,0 span 2 columns, 1 row grid.add(scenetitle, 0, 0, 2, 1);

// Create Label Label userName = new Label("User Name:"); // Add label to grid 0,1 grid.add(userName, 0, 1);

// Create Textfield TextField userTextField = new TextField(); // Add textfield to grid 1,1 grid.add(userTextField, 1, 1);

// Create Label Label pw = new Label("Password:"); // Add label to grid 0,2 grid.add(pw, 0, 2);

// Create Passwordfield PasswordField pwBox = new PasswordField(); // Add Password field to grid 1,2 grid.add(pwBox, 1, 2);

// Create Login Button Button btn = new Button("Login"); // Add button to grid 1,4 grid.add(btn, 1, 4);

final Text actiontarget = new Text(); grid.add(actiontarget, 1, 6);

// Set the Action when button is clicked btn.setOnAction(new EventHandler() {

@Override public void handle(ActionEvent e) { // Authenticate the user boolean isValid = authenticate(userTextField.getText(), pwBox.getText()); // If valid clear the grid and Welcome the user if (isValid) { grid.setVisible(false); GridPane grid2 = new GridPane(); // Align to Center // Note Position is geometric object for alignment grid2.setAlignment(Pos.CENTER); // Set gap between the components // Larger numbers mean bigger spaces grid2.setHgap(10); grid2.setVgap(10); Text scenetitle = new Text("Welcome " + userTextField.getText() + "!"); // Add text to grid 0,0 span 2 columns, 1 row grid2.add(scenetitle, 0, 0, 2, 1); Scene scene = new Scene(grid2, 500, 400); primaryStage.setScene(scene); primaryStage.show(); // If Invalid Ask user to try again } else { final Text actiontarget = new Text(); grid.add(actiontarget, 1, 6); actiontarget.setFill(Color.FIREBRICK); actiontarget.setText("Please try again."); }

} }); // Set the size of Scene Scene scene = new Scene(grid, 500, 400); primaryStage.setScene(scene); primaryStage.show(); }

/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }

/** * @param user the username entered * @param pword the password entered * @return isValid true for authenticated */ public boolean authenticate(String user, String pword) { boolean isValid = false; if (user.equalsIgnoreCase("sdevadmin") && pword.equals("425!pass")) { isValid = true; }

return isValid; }

}

Java mail file

package javamail;

import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;

/** * @description This program uses Java to send emails over the SSL protocol. * */ public class JavaMail {

/** * @param args the command line arguments */ public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.aol.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username","password"); } });

try {

Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@email.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@email.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + " No spam to my email, please!");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) { throw new RuntimeException(e); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!