#8: Implement Login Logic
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
parent
375fbe4826
commit
b9eae8088f
3 changed files with 93 additions and 14 deletions
|
@ -1,6 +1,7 @@
|
||||||
package de.hitec.nhplus;
|
package de.hitec.nhplus;
|
||||||
|
|
||||||
import de.hitec.nhplus.datastorage.ConnectionBuilder;
|
import de.hitec.nhplus.datastorage.ConnectionBuilder;
|
||||||
|
import de.hitec.nhplus.login.LoginController;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
@ -34,8 +35,8 @@ public class Main extends Application {
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
this.primaryStage = primaryStage;
|
this.primaryStage = primaryStage;
|
||||||
//executePassword();
|
executePassword();
|
||||||
executeMainApplication();
|
//executeMainApplication();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executePassword() {
|
private void executePassword() {
|
||||||
|
@ -44,10 +45,19 @@ public class Main extends Application {
|
||||||
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/login/LoginView.fxml"));
|
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/login/LoginView.fxml"));
|
||||||
BorderPane pane = loader.load();
|
BorderPane pane = loader.load();
|
||||||
Scene scene = new Scene(pane);
|
Scene scene = new Scene(pane);
|
||||||
this.primaryStage.setTitle("NHPlus");
|
Stage loginStage = new Stage();
|
||||||
this.primaryStage.setScene(scene);
|
loginStage.setTitle("NHPlus");
|
||||||
this.primaryStage.setResizable(true);
|
loginStage.setScene(scene);
|
||||||
this.primaryStage.show();
|
loginStage.setResizable(false);
|
||||||
|
|
||||||
|
LoginController controller = loader.getController();
|
||||||
|
controller.initialize(loginStage);
|
||||||
|
|
||||||
|
loginStage.showAndWait();
|
||||||
|
|
||||||
|
if(controller.loginSucessfull){
|
||||||
|
executeMainApplication();
|
||||||
|
}
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,63 @@
|
||||||
package de.hitec.nhplus.login;
|
package de.hitec.nhplus.login;
|
||||||
|
|
||||||
|
import de.hitec.nhplus.datastorage.DaoFactory;
|
||||||
|
import de.hitec.nhplus.login.database.UserDao;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.scene.control.PasswordField;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
|
||||||
public void initialize() {
|
@FXML
|
||||||
|
public TextField textFieldUsername;
|
||||||
|
@FXML
|
||||||
|
public PasswordField passwordField;
|
||||||
|
private Stage stage;
|
||||||
|
public boolean loginSucessfull = false;
|
||||||
|
private int loginTries = 0;
|
||||||
|
|
||||||
|
public void initialize(Stage stage) {
|
||||||
|
this.stage = stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleWrongPasswordOrUsername(){
|
||||||
|
loginTries++;
|
||||||
|
if(loginTries == 3){
|
||||||
|
stage.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void handleSubmit() {
|
||||||
|
UserDao dao = DaoFactory.getInstance().createUserDAO();
|
||||||
|
try {
|
||||||
|
int id = dao.readUserId(textFieldUsername.getText());
|
||||||
|
if (id == 0) {
|
||||||
|
handleWrongPasswordOrUsername();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
byte[] salt = dao.readPasswordSalt(id);
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA-512");
|
||||||
|
md.update(salt);
|
||||||
|
byte[] hash = md.digest(passwordField.getText().getBytes(StandardCharsets.UTF_8));
|
||||||
|
byte[] requiredHash = dao.readPasswordHash(id);
|
||||||
|
if (!Arrays.equals(hash, requiredHash)) {
|
||||||
|
handleWrongPasswordOrUsername();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
loginSucessfull = true;
|
||||||
|
stage.close();
|
||||||
|
} catch (SQLException | NoSuchAlgorithmException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.geometry.*?>
|
<?import javafx.geometry.Insets?>
|
||||||
<?import javafx.scene.control.*?>
|
<?import javafx.scene.control.Button?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.control.PasswordField?>
|
||||||
|
<?import javafx.scene.control.TextField?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?import javafx.scene.layout.*?>
|
||||||
|
|
||||||
<BorderPane
|
<BorderPane
|
||||||
xmlns="http://javafx.com/javafx/17.0.2-ea"
|
xmlns="http://javafx.com/javafx/17.0.2-ea"
|
||||||
xmlns:fx="http://javafx.com/fxml/1"
|
xmlns:fx="http://javafx.com/fxml/1"
|
||||||
|
@ -22,11 +24,23 @@
|
||||||
<BorderPane.margin>
|
<BorderPane.margin>
|
||||||
<Insets bottom="8.0" top="8.0"/>
|
<Insets bottom="8.0" top="8.0"/>
|
||||||
</BorderPane.margin>
|
</BorderPane.margin>
|
||||||
<TextField promptText="nutzername" VBox.vgrow="ALWAYS"/>
|
<TextField
|
||||||
<PasswordField promptText="password" VBox.vgrow="ALWAYS"/>
|
fx:id="textFieldUsername"
|
||||||
|
promptText="nutzername"
|
||||||
|
VBox.vgrow="ALWAYS"
|
||||||
|
/>
|
||||||
|
<PasswordField
|
||||||
|
fx:id="passwordField"
|
||||||
|
promptText="password"
|
||||||
|
VBox.vgrow="ALWAYS"
|
||||||
|
/>
|
||||||
</VBox>
|
</VBox>
|
||||||
</center>
|
</center>
|
||||||
<bottom>
|
<bottom>
|
||||||
<Button text="Bestätigen" BorderPane.alignment="CENTER" />
|
<Button
|
||||||
|
text="Bestätigen"
|
||||||
|
BorderPane.alignment="CENTER"
|
||||||
|
onAction="#handleSubmit"
|
||||||
|
/>
|
||||||
</bottom>
|
</bottom>
|
||||||
</BorderPane>
|
</BorderPane>
|
||||||
|
|
Loading…
Reference in a new issue