因此,我的程序可以使用保存在数据库中的信息进行注册和登录。 正如您所知,我几乎没有编码知识,实际上,我花了一个星期的时间编写了这段简单的代码...
现在,我之所以写这篇文章,是因为我需要哈希部分的帮助,尽管我已经看过多个视频并访问了类似的问题,但我仍然受困,仍然无法理解这一部分。
我要做的就是将用户输入的密码从“注册”表单保存到我的数据库中(经过哈希处理或加密),然后通过将普通密码与数据库中的散列密码(散列密码)进行比较来使用普通密码登录。或使用的加密方法都没有关系)。这是我要添加到家庭作业项目中的最后一件事,那就是我的老师没有教给我们任何东西,而我正在借助该网站和youtube自己学习。
非常感谢您的帮助。谢谢。
package sistemaricardo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author Ricardo
*/
public class RegisterForm extends javax.swing.JFrame {
Connection con = null;
PreparedStatement pst=null;
ResultSet rs = null;
/**
* Creates new form RegisterForm
*/
public RegisterForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
registrar = new javax.swing.JButton();
user = new javax.swing.JTextField();
password = new javax.swing.JPasswordField();
jComboBox1 = new javax.swing.JComboBox<>();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
private void registrarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String uname = user.getText();
String pass = String.valueOf(password.getPassword());
if(uname.equals(""))
{
JOptionPane.showMessageDialog(null, "Add username");
}
else if(pass.equals(""))
{
JOptionPane.showMessageDialog(null, "Add password");
}
else{
PreparedStatement ps = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/sistemaricardo?useTimezone=true&serverTimezone=UTC","root","");
String query = "INSERT INTO `multiuserlogin`(`username`, `password`,`usertype`) VALUES (?,?,?)";
pst=con.prepareStatement(query);
pst.setString(1, user.getText());
pst.setString(2, password.getText());
pst.setString(3, String.valueOf(jComboBox1.getSelectedItem()));
pst.executeUpdate();
logindatabase lgf = new logindatabase();
lgf.setVisible(true);
lgf.pack();
lgf.setLocationRelativeTo(null);
lgf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
} catch (SQLException ex) {
Logger.getLogger(RegisterForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void userActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void passwordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
logindatabase lgf = new logindatabase();
lgf.setVisible(true);
lgf.pack();
lgf.setLocationRelativeTo(null);
lgf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPasswordField password;
private javax.swing.JButton registrar;
private javax.swing.JTextField user;
// End of variables declaration
}
像这样的大多数示例都是手动使用预定义的密码,我需要使用用户输入的密码:
require_once("password.php");
$password = "HelloStackOverflow";
$hash = password_hash($password, PASSWORD_BCRYPT);
$hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 10));
if (password_verify($password, $hash)) {
/* Valid */
} else {
/* Invalid */
}