如何使用JDBC(PostgreSQL)将登录凭据与数据库进行比较

时间:2014-03-20 15:01:16

标签: java database postgresql jdbc messenger

所以我试图在Eclipse中创建一个简单的聊天信使。目前,我对如何将用户登录详细信息与存储在数据库表中的详细信息进行比较感到困惑。我有一个'askName()'方法,它接受用户名和密码,这需要使用我的'authenticate'方法来检查数据库中Login表中存储的详细信息。

我一直在这里和网上搜索,并找到了很多代码,这些代码对于获取想法非常有用。然而,我一直在反对这一点,我仍然是编程的新手,任何帮助将不胜感激!

所以目前在Client类中我有这些登录方法:

public void askName()
{
    // get the clients name
    boolean b = true;
    // loop in case they enter a null name - aint nobody got time for that
    while(b == true)
    {
        out.println("What is your name?");
        String name = null;
        try {
            name = in.readLine();
        } catch (IOException e) {
            System.out.println("Can't read name");
        }           
        out.println("What is your password?");
        String password = null;
        try {
            password = in.readLine();
        } catch (IOException e) {
            System.out.println("Can't read password");
        }           
        if(name != null && password != null)
        {
            if(Authenticate(name, password))
            {
                this.username = name;
                b = false;
                out.println("Welcome " + this.username);
            } else {
                out.println("Invalid username/password combination!");
            }   
        }
        else
        {
            out.println("Please enter a valid username and password. Your name must contain at least one letter");
        }   
    }
}

private boolean Authenticate(String name, String password) { // added by Alex 18/03/14
    // Method to check the entered username/password is valid against the database

    // No database class at this point

    //Database database = new Database(); 

    return database.Authenticate(name, password);

}

我已经设法在类'MyJDBC'中实现数据库,只需要弄清楚如何使用我的'authenticate'方法来检查细节。我正在使用PostgreSQL。这是我用于检查登录凭据的当前查询“SELECT password FROM userlogin WHERE username ='”+ name +“'”

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyJDBC {
    public static void main(String args[]) {
        System.out.println("PostgreSQL JDBC Connection Testing");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your PostgreSQL JDBC Driver? "
                    + "Include in your library path!");
            e.printStackTrace();
            return;
        }
        System.out.println("PostgreSQL JDBC Driver Registered!");
        Connection connection = null;
        Statement stmt = null;
        try {
            connection = DriverManager.getConnection(
"jdbc:postgresql://dbteach2.cs.bham.ac.uk:5432/user",
"username",
"password");
            stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery( "SELECT password FROM userlogin WHERE username ='"+name+"'");
            while (rs.next()) {
                String Username = rs.getString("Username");
                String Password = rs.getString("Password");
                int sid = rs.getInt("sid");
                System.out.println("Username = " + Username);
                System.out.println("Password = " + Password);
                System.out.println("sid = " + sid);
                System.out.println();
            }
            rs.close();
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }
    }}

我不是要求任何人做我的代码,但是对于我能做什么/我做错了什么的一些建议将不胜感激。如果你想看到我的其余代码试一试,请说出来。

2 个答案:

答案 0 :(得分:2)

在实践中,你显然不应该使用明文密码,你应该哈希并加盐。然后将相同的散列和盐应用于输入的密码并匹配它们,就像我将在下面描述的那样。挺直的。

当你要求了解基本过程时,这里是:
检索userlogin的密码,userlogin应为unique,因此ResultSet中只会有一个元组。如果没有检索到username的行,则该用户不存在 - >扔一个Exception

接下来是将您检索到的密码与用户通过retrievedPw.equals(userEnteredPw)输入的密码进行匹配。如果返回true,则在用户凭据正确时将用户登录。

如果谈到腌制,我自己也会在这个问题上挣扎,所以你可能想要阅读This disucssion on salting best practices.

答案 1 :(得分:0)

我的建议:

  • 出于安全原因在数据库中添加哈希密码
  • 使用预准备语句来避免SQL注入攻击
  • 添加适当的资源关闭以避免生产上的麻烦