Added changePasswd()

This commit is contained in:
2022-06-10 01:12:27 +08:00
parent 2ad91665bc
commit c5fb75f515
3 changed files with 46 additions and 0 deletions

View File

@@ -191,6 +191,10 @@ public class DatabaseHelper {
return StudentOA.checkPasswd(stuID, passwd);
}
public static boolean changePasswdInStudent(long stuID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
return StudentOA.changePasswd(stuID, passwd);
}
public static Student insertIntoStudent(long stuID, String stuName, Gender gender, long classID, String passwd) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
return StudentOA.insert(stuID, stuName, gender, classID, passwd);
}
@@ -215,6 +219,10 @@ public class DatabaseHelper {
return TeacherOA.checkPasswd(tchID, passwd);
}
public static boolean changePasswdInTeacher(long tchID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
return TeacherOA.changePasswd(tchID, passwd);
}
public static Teacher insertIntoTeacher(long tchID, String tchName, Gender gender, int facID, String passwd) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
return TeacherOA.insert(tchID, tchName, gender, facID, passwd);
}

View File

@@ -56,6 +56,25 @@ public class StudentOA {
return pbkdf2Util.authenticate(passwd, student.getPassword(), student.getSalt());
}
public static boolean changePasswd(long stuID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
Student student = select(stuID);
if (student == null) {
throw new DependenciesNotFoundException();
}
PBKDF2Util pbkdf2Util = new PBKDF2Util();
String salt = pbkdf2Util.generateSalt();
String encryptedPassword = pbkdf2Util.getEncryptedPassword(passwd, salt);
String sql = "UPDATE student SET passwd=?,salt=? WHERE stuID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, encryptedPassword);
preparedStatement.setString(2, salt);
preparedStatement.setLong(3, stuID);
return preparedStatement.executeUpdate() == 1;
}
}
}
public static Student insert(long stuID, String stuName, Gender gender, long classID, String passwd) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException, NoSuchAlgorithmException, InvalidKeySpecException {
if (isExists(stuID)) throw new AlreadyExistsException();
if (!ClassOA.isExists(classID)) throw new DependenciesNotFoundException();

View File

@@ -52,6 +52,25 @@ public class TeacherOA {
return pbkdf2Util.authenticate(passwd, teacher.getPassword(), teacher.getSalt());
}
public static boolean changePasswd(long tchID, String passwd) throws NoConfigException, SQLException, DependenciesNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
Teacher teacher = select(tchID);
if (teacher == null) {
throw new DependenciesNotFoundException();
}
PBKDF2Util pbkdf2Util = new PBKDF2Util();
String salt = pbkdf2Util.generateSalt();
String encryptedPassword = pbkdf2Util.getEncryptedPassword(passwd, salt);
String sql = "UPDATE teacher SET passwd=?,salt=? WHERE tchID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, encryptedPassword);
preparedStatement.setString(2, salt);
preparedStatement.setLong(3, tchID);
return preparedStatement.executeUpdate() == 1;
}
}
}
public static Teacher insert(long tchID, String tchName, Gender gender, int facID, String passwd) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException, NoSuchAlgorithmException, InvalidKeySpecException {
if (isExists(tchID)) throw new AlreadyExistsException();
if (!FacultyOA.isExists(facID)) throw new DependenciesNotFoundException();