java - 为我的应用创建管理员
问题描述
因此,如果用户在 db 中的列 admin 1 而不是 0 处加载不同的 fxml 文件,管理员可以在其中执行比普通用户更多的操作。
这是负责用户登录的控制器,测试密码和用户名是否在数据库中。
PS 更多关于我如何尝试在下面制作方法的评论。
public class LogareController implements Initializable {
LoginVerifier loginVerifier = new LoginVerifier();
@FXML
private TextField Numeutilzator;
@FXML
private PasswordField Parola;
@FXML
private Label Stare;
@Override
public void initialize(URL location, ResourceBundle resources) {
if (loginVerifier.Conexiune()) {
Stare.setText("");
} else {
Stare.setText("Conexiune nereusita!");
}
}
public void Autentificare (ActionEvent event) {
try {
if(loginVerifier.testaredate(Numeutilzator.getText(), Parola.getText())) {
Stare.setText("Autentificare reusita !");
((Node)event.getSource()).getScene().getWindow().hide();
Stage st= new Stage();
FXMLLoader loader= new FXMLLoader();
Pane Pane = loader.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());
Scene scene = new Scene(Pane);
scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
st.setScene(scene);
st.show();
}
else {
Stare.setText("Nume de utilizator sau parola incorect");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void Inregistrare(ActionEvent event) {
try {
((Node)event.getSource()).getScene().getWindow().hide();
Stage PS= new Stage();
FXMLLoader loader= new FXMLLoader();
Pane Pane1 = loader.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
Scene scena = new Scene(Pane1);
scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
PS.setScene(scena);
PS.show();
} catch (Exception e) {
}
}
// This is what I came up with, I know its bad, but I can't think of anything else, in this method I am trying to save the value of "Admin" from the table in a variable, which I can you to determen if the user has 0 or 1 at the admin, If he has 1 then a new fxml will be loaded for him, if he has 0 he is a
regular user
public boolean admin(int admin) throws SQLException {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
PreparedStatement PSMG= null;
ResultSet RSMG = null;
String Interogare = "SELECT Admin FROM accounts where Admin='1'";
try {
PSMG = conectare.prepareStatement(Interogare);
PSMG.setLong(1, admin);
LogareController Adminstatus = new LogareController();
String Adminstatus = admin.getBytes() //IT only lets me to use getBytes(), i wanted to get the value from admin, after the query executed, this causes a confict with the primitive type "int".
} catch (Exception exceptie2) {
return true;
}
return false;
}
}
}
基本上,我如何创建一种方法,在该方法中我可以从 sql 中保存 admin 的值,然后在测试登录凭据以创建一个“if”条件时,该条件将确定应该加载什么 fxml。
登录验证器
public class LoginVerifier {
public LoginVerifier () {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
if (conectare == null) {
System.out.println("Conectare nereusita!");
System.exit(1);}
}
public boolean Conexiune() {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
try {
return !conectare.isClosed();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean testaredate(String numeutil, String parola) throws SQLException {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
PreparedStatement PSMG= null;
ResultSet RSMG = null;
String Interogare = "SELECT * FROM accounts where Username=? and Password=?";
try {
PSMG = conectare.prepareStatement(Interogare);
PSMG.setString(1, numeutil);
PSMG.setString(2, parola);
RSMG = PSMG.executeQuery();
if(RSMG.next()){
return true;
}
else {
return false;
}
} catch (Exception exceptie2) {
return false;
}
}
}
解决方案
这是一个如何实现它的粗略示例。
User
类是描述用户的模型。我刚刚将它们与其他 fx 组件兼容。
public class User {
private IntegerProperty id = new SimpleIntegerProperty();
private StringProperty name = new SimpleStringProperty();
private StringProperty password = new SimpleStringProperty();
private BooleanProperty admin = new SimpleBooleanProperty();
public int getId() {
return id.get();
}
public IntegerProperty idProperty() {
return id;
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getPassword() {
return password.get();
}
public StringProperty passwordProperty() {
return password;
}
public void setPassword(String password) {
this.password.set(password);
}
public boolean isAdmin() {
return admin.get();
}
public BooleanProperty adminProperty() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin.set(admin);
}
}
User
-class 对象的创建和初始化在LoginVerifier#login
函数中完成
public class LoginVerifier {
public Optional<User> login(String username, String password) throws SQLException {
ConectaredB ConectaredB = new ConectaredB();
Connection conectare = ConectaredB.logareDB();
PreparedStatement PSMG = null;
ResultSet RSMG = null;
String Interogare = "SELECT * FROM accounts where Username = ? and Password = ?";
PSMG = conectare.prepareStatement(Interogare);
PSMG.setString(1, username);
PSMG.setString(2, password);
RSMG = PSMG.executeQuery();
if(RSMG.next()) {
User user = new User();
user.setName(username);
user.setPassword(password);
user.setId(RSMG.getInt("id"));
user.setAdmin(RSMG.getInt("Admin") == 1);
return Optional.of(user);
}
return Optional.empty();
}
}
通过检查是否Optional
包含对象来完成身份验证,并通过检查User#isAdmin
属性来进行授权
public void Autentificare(ActionEvent event) {
try {
Optional<User> optional = loginVerifier.login(Numeutilzator.getText(), Parola.getText());
if(optional.isPresent()) {
String fxmlFile = optional.get().isAdmin() ? "/LicentaApp/Meniu_Admin.fxml" : "/LicentaApp/Meniu.fxml";
Stare.setText("Autentificare reusita !");
((Node)event.getSource()).getScene().getWindow().hide();
Stage st = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane Pane = loader.load(getClass().getResource(fxmlFile).openStream());
Scene scene = new Scene(Pane);
scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
st.setScene(scene);
st.show();
}
else {
Stare.setText("Nume de utilizator sau parola incorect");
}
}
catch (SQLException | IOException e) {
e.printStackTrace();
}
}
推荐阅读
- javascript - Proxyquire 显示错误“找不到模块”
- swift - 如何从 Firestore 获取数组字段并在 Swift 中写入 struct 字段?
- windows - 如何使用 nuget.exe 从 artifactory 下载受保护的(私有)nuget 包
- javascript - 如何绘制像两个或多个表通过 D3.js 中的公共值连接的图形?
- r - 如何使系列静止
- java - 为什么返回值对象在 URLClassPath 的 getResources 函数中有两个额外的字段?
- django - 从 prepare_field 返回字典
- git - Sublime Text 3.2 中的 GIT 集成仍然出现
- hadoop - 无法写入 HDFS:WARN hdfs.DataStreamer - Unexpected EOF
- django - 如何在friends_list/followers_list中显示不同的头像,我正在练习django