gpt4 book ai didi

java - 使用 JDBC 实现 gui 的用户名和密码检查,但是

转载 作者:行者123 更新时间:2023-11-29 08:22:09 25 4
gpt4 key购买 nike

我正在使用 JDBC 检查数据库中的用户名和密码,以授予对我的 gui 的登录访问权限,但是当我尝试测试 JDBC 是否正常工作时,当输入错误的用户名和密码时,它不会阻止访问..

下面是我的代码,我相信它正确连接了数据库,因为当我按下登录按钮时,它输出运行检查登录并且用户已验证。

class usernamecheck {

static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
static final String USERNAME = "root";
static final String PASSWORD = "root";

// launch the application
public static boolean checkLogin(String username, String password)
throws SQLException {
System.out.print("Running Check Login \n");

Connection connection = null; // manages connection
PreparedStatement pt = null; // manages prepared statement

// connect to database usernames and query database
try {

// establish connection to database
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(DATABASE_URL, "root", "root");

// query database
pt = con.prepareStatement("select userName,password from mysql.person where userName=?");

// process query results
pt.setString(1, username);
ResultSet rs = pt.executeQuery();
String orgUname = "", orPass = "";
while (rs.next()) {
orgUname = rs.getString("userName");
orPass = rs.getString("password");
} //end while
if (orPass.equals(password)) {
//do something
return true;
} else {
//do something
}
}//end try
catch (Exception e) {
} //end catch
return false;
} //end main
}

使用 GUI 编写代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;


public class Login extends JFrame {

private JTextField jtfUsername, jtfPassword;
private JButton backButton, loginButton;
private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;

Login() {
//create menu bar
JMenuBar jmb = new JMenuBar();

//set menu bar to the applet
setJMenuBar(jmb);

//add menu "operation" to menu bar
JMenu optionsMenu = new JMenu("Options");
optionsMenu.setMnemonic('O');
jmb.add(optionsMenu);

//add menu "help"
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
jmb.add(helpMenu);

//add menu items with mnemonics to menu "options"
optionsMenu.add(jmiLogin = new JMenuItem("Login", 'L'));
optionsMenu.addSeparator();
optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));

//panel p1 to holds text fields
JPanel p1 = new JPanel(new GridLayout(2, 2));
p1.add(new JLabel("Username"));
p1.add(jtfUsername = new JTextField(15));
p1.add(new JLabel("Password"));
p1.add(jtfPassword = new JPasswordField(15));

//panel p2 to holds buttons
JPanel p2 = new JPanel(new FlowLayout());
p2.add(backButton = new JButton("Back"));
p2.add(loginButton = new JButton("Login"));

//Panel with image??????

//add panels to frame
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(p1, BorderLayout.CENTER);
panel.add(p2, BorderLayout.SOUTH);
add(panel, BorderLayout.CENTER);
setTitle("Main Page");


//listners for exit menuitem and button
jmiBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome welcome = new Welcome();
welcome.setVisible(true);
welcome.setSize(500, 500);
welcome.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
Login.this.dispose();
Login.this.setVisible(false);
}
});

backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome welcome = new Welcome();
welcome.setVisible(true);
welcome.setSize(500, 500);
welcome.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
Login.this.dispose();
Login.this.setVisible(false);
}
});

//listner for about menuitem
jmiAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This is the login panel"
+ "\n Assignment for University",
"About", JOptionPane.INFORMATION_MESSAGE);
}
});

//action listeners for Login in button and menu item
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
usernamecheck.checkLogin(jtfUsername.getText(), jtfPassword.getText()); {
System.out.println("User is validated");
}
} catch (SQLException se) {
}
MainMenu mainmenu = new MainMenu();
mainmenu.setVisible(true);
mainmenu.setSize(500, 500);
mainmenu.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
Login.this.dispose();
Login.this.setVisible(false);
}
});

jmiLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu mainmenu = new MainMenu();
mainmenu.setVisible(true);
mainmenu.setSize(500, 500);
mainmenu.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
Login.this.dispose();
Login.this.setVisible(false);
}
});
}

public static void main(String arg[]) {
Login frame = new Login();
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class usernamecheck {

static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
static final String USERNAME = "root";
static final String PASSWORD = "root";


// launch the application
public static boolean checkLogin(String username, String password)
throws SQLException {
System.out.print("Running Check Login \n");

Connection connection = null; // manages connection
PreparedStatement pt = null; // manages prepared statement
Statement stmt = null;
String query="select userName from person where userName = ? and password = ?";


// connect to database usernames and query database
try {

// establish connection to database
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(DATABASE_URL, "root", "root");

// query database
pt = con.prepareStatement("select userName from person where userName = ? and password = ?");

// process query results
pt.setString(1, username);
ResultSet rs = pt.executeQuery(query);
String orgUname = "", orPass = "";
while (rs.next()) {
orgUname = rs.getString("userName");
orPass = rs.getString("password");
} //end while
if (orPass.equals(password) && orgUname.equals(username)) {
//do something
return false;
} else {
//do something
return true;
}
}//end try
catch (Exception e) {
} //end catch
return true;
} //end main
}

最佳答案

这里有一些建议:

不要将密码存储在数据库中。存储它的 MD5 哈希值,然后让 Java 代码或 Mysql 函数将用户的密码输入文本转换为 MD5 哈希值,然后将其与 person 表中存储的内容进行比较。

使用 Java 进行哈希处理的示例:

表:

+----+------------+----------------------------------+
| id | username | pwhash |
+----+------------+----------------------------------+
| 1 | bob | 9ae4f6963062ba8c77db69aa1f821310 |
| 2 | ryan | 3f6af9632621a8ce7d00aa122e2d1310 |
+----+------------+----------------------------------+

Java代码:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

....

String username = ... // from UI input
String plaintext_password = ... // from UI input

String pwhash_from_passwd = makePwHash(username, plaintext_password);

String pwhash_from_db = ... // SELECT pwhash FROM person WHERE userName=?

if (pwhash_from_db.equals(pw_hash_from_passwd)) {
// user is authenticated
} else {
// invalid username or password
}

...



protected static String makePwHash(String username, String plaintext_password) {
MessageDigest mdigest=null;
try {
mdigest = MessageDigest.getInstance("MD5");
String dbs = username + plaintext_password;
byte mdbytes[] = mdigest.digest(dbs.getBytes());
return toHexString(mdbytes);
} catch (NoSuchAlgorithmException e) { }
return null;
}



private static final char[] toHex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

/**
* convert an array of bytes to an hexadecimal string
* @return a string (length = 2 * b.length)
* @param b bytes array to convert to a hexadecimal string
*/
public static String toHexString(byte b[]) {
int pos = 0;
char[] c = new char[b.length*2];
for (int i=0; i< b.length; i++) {
c[pos++] = toHex[(b[i] >> 4) & 0x0F];
c[pos++] = toHex[b[i] & 0x0f];
}
return new String(c);
}

关于java - 使用 JDBC 实现 gui 的用户名和密码检查,但是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19056381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com