사용자 이름, 비밀번호 및 유형의 3 가지 입력 필드가 있습니다.
3 개 중 하나가 잘못된 경우 "usern & pass is not correct"라는 오류 메시지가 1 개만 표시됩니다.
비밀번호, 사용자 이름 또는 유형 값이 잘못 입력되었는지 여부를 표시하도록 오류 메시지를 분석하려면 어떻게해야합니까?
내가 사용한 두 파일은 Login Model과 LoginController입니다. BTW, 아래 코드에는 오류가 없습니다.
완벽하게 작동합니다. 오류 메시지를 분석하기 위해 확장하고 싶습니다.
LoginController 파일 :
public class LoginController implements Initializable {
/**
* Initializes the controller class.
*/
public LoginModel loginModel = new LoginModel();
@FXML private Label isConnected;
@FXML private JFXTextField txtUsername;
@FXML private JFXPasswordField txtPassword;
@FXML private ComboBox<String> comboType;
ObservableList<String> list = FXCollections.observableArrayList("admin", "manager", "clerk");
@Override
public void initialize(URL url, ResourceBundle rb) {
comboType.setItems(list);
if(loginModel.isDbConnected()) {
isConnected.setText("Connected");
}
else {
isConnected.setText("Not Connected");
}
}
public void Login (ActionEvent event) {
try {
if(loginModel.isLogin(comboType.getValue(), txtUsername.getText(), txtPassword.getText())) {
isConnected.setText("usern & pass is correct");
//closes login fxml file
((Node)event.getSource()).getScene().getWindow().hide();
//loads main interface fxml file
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("/gui/uicomponents/Main.fxml").openStream());
MainController mainController = (MainController)loader.getController();
mainController.getUser("Hi " + txtUsername.getText());
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/resources/css/Consolidated.css").toExternalForm());
primaryStage.setMaximized(true);
primaryStage.setTitle("Main Interface");
primaryStage.setScene(scene);
primaryStage.show();
}
else {
isConnected.setText("usern & pass is not correct");
}
} catch (SQLException ex) {
isConnected.setText("usern & pass is not correct");
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
LoginModel 파일 :
public class LoginModel {
Connection connection;
public LoginModel() {
connection = SqliteConnection.Connector();
if(connection == null) {
System.out.println("Connection to DB Failed");
System.exit(1);
}
}
public boolean isDbConnected() {
try {
return !connection.isClosed();
} catch (SQLException ex) {
Logger.getLogger(LoginModel.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean isLogin(String type, String user, String pass) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query = "select * from users where type = ? and username = ? and password = ?";
try {
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, type);
preparedStatement.setString(2, user);
preparedStatement.setString(3, pass);
resultSet = preparedStatement.executeQuery();
//if returns result from db
if(resultSet.next()) {
return true;
}
else {
return false;
}
}
catch(Exception e) {
System.out.println(e);
return false;
}
finally {
preparedStatement.close();
resultSet.close();
}
}
}
그것은 보안의 관점에서 좋은 방법이 아닙니다 밝힐 것을하는 세부 사항의 부분, 즉, 당신이 말을 더 힌트를 제공하고 있기 때문에 해커에게 응용 프로그램이 취약 부정확하게된다는이 있는지 userid
정확하지 않거나 password
올바르지 않거나 type
올바르지 않습니다 .
그러나 자신의 프로젝트 요구 사항으로 인해 매우 구체적인 오류 메시지를 표시하려는 경우에는 Type
and UserName
exist 또는 NOT 을 확인한 다음 아래와 같이 사용자 지정 예외 TypeNotFoundException
또는 UserNameNotFoundException
예외와 같은 사용자 지정 예외를 정의하고 throw하여 이를 달성 할 수 있습니다.
Login () 메서드 :
public void Login (ActionEvent event) {
try {
if(loginModel.isLogin(comboType.getValue(),
txtUsername.getText(), txtPassword.getText())) {
//add your code
}
else {
isConnected.setText(" pass is not correct");
}
} catch (TypeNotFoundException ex) {
isConnected.setText("Type is not correct");
//add your logger
} catch (UserNameNotFoundException ex) {
isConnected.setText("Username is not correct");
//add your logger
} catch (SQLException ex) {
isConnected.setText("technical problem,please try after some time");
//add your logger
} catch (IOException ex) {
//add your logger
}
}
isLogin () :
public boolean isLogin(String type, String user, String pass) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query = "select * from users where type = ?
and username = ? and password = ?";
try {
//other code
//if returns result from db
if(resultSet.next()) {
return true;
}
else {
//String typeQuery = "select * from users where type = ?";
//Execute the typeQuery and throw TypeNotFoundException
//String userNameQuery = "select * from users where username = ?";
//Execute the userNameQuery and throw UserNameNotFoundException
return false;
}
} //other code as is
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다