I am creating a web-based login system and for this I use MySQL as my backend, JSPS and Servlets for functionality, also using the jasypt 1.8 library to encrypt passwords so that I can store them in MySQL later.
My question is: is there anything wrong with the following code (security related, best practices, etc.):
protected boolean verifyUser(String user, String pass) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor(); Connection conn = null; String userName = "****"; String password = "****"; String url = "jdbc:mysql://localhost:3306/DB"; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, userName, password); System.out.println("Database connection established"); PreparedStatement stmt = null; stmt = conn.prepareStatement("SELECT * FROM DB.LOGINS WHERE USER = ?"); stmt.setString(1, user); rs = stmt.executeQuery(); if(!rs.next()){ return false; } if(passwordEncryptor.checkPassword(pass, rs.getString("Password"))){ return true; } conn.close(); } catch (Exception e) { } return false; }
I donβt know if itβs right to get the user out of the database and then compare it with the password provided (i.e., I think the best way would be to get information from the database using the username and password, and not just the first one). Unfortunately, I cannot do this because I cannot generate the key, because there is no way in the library that can do this (and I could not find the algorithm in the documentation for generating the key).
I do this because the library I use has a built-in random salt generator that is stored inside the line created after encryption (using sha-256 for this, if that matters ...) and the checkPassword () method is the only thing able to generate a key that is exactly the same as the one stored in the database (which has already passed the encryption process).
In any case, if you have any experience in creating login systems or you know a lot of security recommendations, I would like you to tell me that your experience is related to such problems if you have not found a suitable solution.
Thank you for your time.
source share