DEV Community

Explorer
Explorer

Posted on

🧩 Automate Random Password Generation in Joget Using BeanShell Script

💡 Introduction

Sometimes in Joget applications, user or contact records may be created without passwords — especially during data imports or integrations.

To fix that, we can automate password creation using a BeanShell script, which directly connects to Joget’s database and assigns secure random passwords to any empty password fields.


🎯 Goal

Automatically generate a secure, random password for every record in the app_fd_Contacts table where c_Password is NULL.


⚙️ How It Works

This BeanShell script:

  1. Connects to Joget’s database using AppUtil.
  2. Finds all records missing passwords.
  3. Generates a random secure password.
  4. Updates the database with the new passwords.
  5. Logs the output for easy tracking.

🧱 BeanShell Script

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Random; import javax.sql.DataSource; import org.joget.apps.app.service.AppUtil; import org.joget.commons.util.LogUtil; // Character sets for password generation private final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private final String CHAR_UPPER = CHAR_LOWER.toUpperCase(); private final String NUMBER = "0123456789"; private final String SPECIAL_CHARS = "!@#$%^&*()_-+=<>?"; private final String ALL_CHARS = CHAR_LOWER + CHAR_UPPER + NUMBER + SPECIAL_CHARS; private final Random RANDOM = new Random(); Connection connection = null; public void RandomPasswordGenerator() { try { // Get Joget database connection from AppUtil DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource"); connection = ds.getConnection(); // Select records with missing passwords String selectQuery = "SELECT id FROM app_fd_Contacts WHERE c_Password IS NULL"; // Update query to assign new passwords String updateQuery = "UPDATE app_fd_Contacts SET c_Password = ? WHERE id = ?"; PreparedStatement selectStatement = connection.prepareStatement(selectQuery); PreparedStatement updateStatement = connection.prepareStatement(updateQuery); ResultSet resultSet = selectStatement.executeQuery(); while (resultSet.next()) { LogUtil.info("", "Inside while loop"); // Get record ID String id = resultSet.getString("id"); // Generate new random password String newPassword = generateRandomPassword(); LogUtil.info("", newPassword); // Update the record with the generated password updateStatement.setString(1, newPassword); updateStatement.setString(2, id); updateStatement.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } finally { // Always close connection in finally block if (connection != null) { try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } } } // Function to generate a secure random password public String generateRandomPassword() { StringBuilder sb = new StringBuilder(8); // Ensure at least one character from each category sb.append(CHAR_LOWER.charAt(RANDOM.nextInt(CHAR_LOWER.length()))); sb.append(CHAR_UPPER.charAt(RANDOM.nextInt(CHAR_UPPER.length()))); sb.append(NUMBER.charAt(RANDOM.nextInt(NUMBER.length()))); sb.append(SPECIAL_CHARS.charAt(RANDOM.nextInt(SPECIAL_CHARS.length()))); // Fill the rest of the password randomly for (int i = 4; i < 8; i++) { int randomIndex = RANDOM.nextInt(ALL_CHARS.length()); sb.append(ALL_CHARS.charAt(randomIndex)); } // Shuffle the generated password for randomness char[] passwordChars = sb.toString().toCharArray(); for (int i = 0; i < passwordChars.length; i++) { int randomIndex = RANDOM.nextInt(passwordChars.length); char temp = passwordChars[i]; passwordChars[i] = passwordChars[randomIndex]; passwordChars[randomIndex] = temp; } return new String(passwordChars); } // Execute the function RandomPasswordGenerator(); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)