根据输入字段分解错误消息

时间:2017-03-18 10:47:14

标签: java javafx

我有3个输入字段:用户名,密码和类型。

如果3个中的任何一个不正确,我只会得到1个错误消息,表明" usern&传球不正确"。

如何分解错误消息,以便向我显示密码,用户名或类型的值是否输入错误。

这些是我使用的2个文件:Login Model和LoginController。顺便说一句,下面的代码没有错误。

完美无缺。我只是想扩展它以分解错误信息。

LoginController文件:

DB string “BMW”
Query string “A BMW is a fancy car”

LoginModel文件:

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

2 个答案:

答案 0 :(得分:2)

从安全角度来看,这不是一个好习惯,以揭示细节的哪一部分是不正确的,即,它会使您的应用程序更容易受到黑客攻击,因为您提供了更多提示userid是否不正确或password不正确或type是否错误。

但是,如果您真的想根据自己的项目要求显示非常具体的错误消息,可以通过检查TypeUserName是否存在然后定义&amp;来实现。抛出自定义异常,例如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 :(得分:1)

简单地说,您的方法 isLogin()需要对传入数据进行更明确的分析。

例如,您可以先检查用户名是否已知。

但是你看到了;从安全角度来看,您可能不希望向用户提供此类详细信息。假设攻击者猜测用户名。当你给他不同的消息时,用户身份不明,密码无效;然后你帮他攻击你。您可以将此类信息放入日志文件中,但不必将所有详细信息都用于使用!

谈论安全性:似乎您打算将密码作为纯文本存储在数据库中。那绝对不行!

但鉴于您的客户直接与数据库通信,安全性似乎并不是您的首要任务。

相关问题