I need to create a security feature for my Javafx Login Gui Application. I need
ID: 3701869 • Letter: I
Question
I need to create a security feature for my Javafx Login Gui Application. I need to enforce unsucessful login attemps by limiting 3 max unsuccessful log ons then system will automatically lock the login account.
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<ActionEvent>() {
@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;
}
}
Explanation / Answer
public class LoginController implements DialogController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private ScreensConfiguration screens;
private AutowireFXMLDialog dialog;
public void setDialog(AutowireFXMLDialog dialog)
{
this.dialog = dialog;
}
@FXML
Label header;
@FXML
TextField username;
@FXML
TextField password;
@FXML
public void login()
{
Authentication authToken = new UsernamePasswordAuthenticationToken(username.getText(),
password.getText());
try
{
authToken = authenticationManager.authenticate(authToken);
SecurityContextHolder.getContext().setAuthentication(authToken);
} catch (AuthenticationException e)
{
header.setText("Login failure, please try again:");
header.setTextFill(Color.DARKRED);
return;
}
dialog.close();
screens.showScreen(screens.customerDataScreen());
}
@FXML
public void employee()
{
username.setText("employee");
password.setText("lol");
}
@FXML
public void manager()
{
username.setText("manager");
password.setText("password");
}
}
===>SECOND PART IS ALSO LOGINPAGE AUTHENTICATION CODE:
LOGIN CONTROLLER CODE
}
2)THEN WE HAVE CREATE THE LOGIN MANAGER PAGE:
/**
MainViewController controller =
==>
WE USE FXML IS LOGIN PAGE
THATS WHY WE USE IT
}
2)THEN WE HAVE CREATE THE LOGIN MANAGER PAGE:
package login; import java.io.IOException; import java.util.logging.*; import javafx.fxml.FXMLLoader; import javafx.scene.*; /** Manages control flow for logins */ public class LoginManager { private Scene scene; public LoginManager(Scene scene) { this.scene = scene; } /** * Callback method invoked to notify that a user has been authenticated. * Will show the main application screen. */ public void authenticated(String sessionID) { showMainView(sessionID); }/**
* Callback method invoked to notify that a user has logged out of the main application. * Will show the login application screen. */ public void logout() { showLoginScreen(); } public void showLoginScreen() { try { FXMLLoader loader = new FXMLLoader( getClass().getResource("login.fxml") ); scene.setRoot((Parent) loader.load()); LoginController controller = loader.<LoginController>getController(); controller.initManager(this); } catch (IOException ex) { Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex); } } private void showMainView(String sessionID) { try { FXMLLoader loader = new FXMLLoader( getClass().getResource("mainview.fxml") ); scene.setRoot((Parent) loader.load());MainViewController controller =
loader.<MainViewController>getController(); controller.initSessionID(this, sessionID); } catch (IOException ex) { Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.