2017-03-18 36 views
0

我有3個輸入字段:用戶名,密碼和類型。基於輸入字段分解錯誤消息

如果其中任何一個不正確,我只會收到1條錯誤消息,其中指出「usern &通過不正確」。

如何分解錯誤消息,以便顯示密碼,用戶名或類型的值是否輸入錯誤。

這些是我使用的2個文件:Login Model和LoginController。順便說一句,下面的代碼沒有錯誤。

它完美的作品。我只是想擴展它來分解錯誤信息。

的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(); 
     } 
    } 
} 

回答

1

它不是從安全角度,要揭示的細節部分一個很好的做法是不正確的,也就是說,它使你的應用程序更容易受到黑客,因爲你是給更多的提示說,是否userid是不正確或password不正確或type不正確。

但是,如果你真的想顯示的非常具體的錯誤消息,因爲自己的項目需求,可以做到這一點通過檢查TypeUserName存在或不是,然後定義&拋出自定義異常像TypeNotFoundExceptionUserNameNotFoundException例外如下圖所示:

登錄()方法:

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 
    } 
1

簡單地說,你的方法isLogin()需要做的輸入數據的多個不同的分析。

例如,您可以首先檢查用戶名是否已知。

但你明白了;從安全的角度來看,您可能不想將這些細節提供給用戶。假設攻擊者是猜測用戶名。當你向用戶發送不同的消息並且密碼無效時;那麼你幫他攻擊你。您可以將這些信息放入日誌文件中,但不必將所有的細節都提供給用戶!

談到安全性:它似乎打算將密碼作爲純文本存儲在數據庫中。這是絕對不行!

但是,考慮到您的客戶直接與數據庫交談,安全似乎不是您的首要任務。

+0

首先感謝您的時間和答覆。另外,當涉及到安全性時,這一點很重要。我應該如何在數據庫中存儲密碼和用戶名?我應該使用第三方軟件加密嗎? – Roger

+0

你應該做一些研究。 「正常」的方法是使用密碼散列函數,您可以使用「salted」密碼。然後將結果存儲在數據庫中。稍後,您只需重複該過程,並在結果與數據庫條目相匹配時,密碼正確。但正如所說:這是基本的東西,你可以讀到你自己... – GhostCat