Password Generator Utility
Relevant source files
The following files were used as context for generating this wiki page:
- Part1/API/src/main/java/com/example/api/PasswordGenerator.java
- Part1/API/src/main/java/com/example/api/config/DataLoader.java
- Part1/API/src/main/java/com/example/api/security/SecurityConfig.java
- Part1/API/src/main/java/com/example/api/entity/AppUser.java
- Part1/API/src/main/java/com/example/api/security/CustomUserDetailsService.java
- Part1/API/src/main/resources/api-spec.yaml
The Password Generator Utility is a standalone developer tool within the SafeDispatch HQ Administration API project designed to facilitate the creation of hashed credentials. Its primary purpose is to generate BCrypt-encoded strings from plain-text passwords, which can then be used for manual database seeding or configuration verification.
This utility ensures that administrative accounts, such as those for HR and Managers, conform to the project's security standards before they are persisted in the system's identity store. By utilizing the same encryption algorithms as the live API, it maintains consistency across the security infrastructure.
Sources: Part1/API/src/main/java/com/example/api/PasswordGenerator.java:1-12, Part1/API/src/main/resources/api-spec.yaml:5-8
Core Implementation logic
The utility is implemented as a simple Java class with a main method, allowing it to be executed independently of the full Spring Boot application context.
Encoding Mechanism
The class instantiates a BCryptPasswordEncoder, which is the standard password hashing function used throughout the application. It specifically generates hashes for two default system roles:
- HR: Encodes the plain-text password "ok".
- MANAGER: Encodes the plain-text password "manager123".
The resulting hashes are printed to the standard output, allowing developers to copy-paste them into SQL scripts or environmental configurations.
Sources: Part1/API/src/main/java/com/example/api/PasswordGenerator.java:5-11
Logic Flow
The following diagram illustrates the simple execution flow of the utility:
The flow shows the sequential process of generating and outputting hashed credentials for default system users.
Sources: Part1/API/src/main/java/com/example/api/PasswordGenerator.java:7-11
Integration with Security Architecture
While the PasswordGenerator is a standalone utility, the logic it uses is central to the application's overall security architecture defined in SecurityConfig.
Password Encoder Configuration
The application defines a PasswordEncoder bean using the BCryptPasswordEncoder implementation. This bean is used by both the security filter chain and the authentication provider to validate user credentials during the login process.
Sources: Part1/API/src/main/java/com/example/api/security/SecurityConfig.java:23-26, Part1/API/src/main/java/com/example/api/security/SecurityConfig.java:29-34
User Data Bootstrapping
The DataLoader component utilizes the same encoding logic to programmatically seed the database with initial users if the UserRepository is empty. This automated process mirrors the manual capability provided by the PasswordGenerator utility.
| User Role | Username | Initial Password (Plain) |
|---|---|---|
| HR | hr | hr123 |
| MANAGER | manager | manager123 |
Sources: Part1/API/src/main/java/com/example/api/config/DataLoader.java:18-30
Data Persistence Model
The hashes generated by this utility are designed to be stored in the AppUser entity. This entity represents the administrative users who have access to the HQ Administration API.
This diagram represents the relationship between the generator utility and the user entity it supports.
Sources: Part1/API/src/main/java/com/example/api/entity/AppUser.java:6-20, Part1/API/src/main/java/com/example/api/PasswordGenerator.java:5-12
Entity Fields
| Field | Type | Constraint | Description |
|---|---|---|---|
| id | Long | Primary Key, Identity | Unique identifier for the user. |
| username | String | Unique, Non-null | The login name for the administrator. |
| password | String | Non-null | The BCrypt hashed password. |
| role | String | Non-null | The access level (e.g., HR or MANAGER). |
Sources: Part1/API/src/main/java/com/example/api/entity/AppUser.java:10-20
Security Context and API Specification
The API is secured using HTTPS and Role-Based Access Control (RBAC). Credentials generated by the utility are used in conjunction with HTTP Basic Authentication to access the management endpoints.
Access Levels
As defined in the API specification and security configuration:
- GET requests to
/api/**are accessible to bothMANAGERandHRroles. - POST/PUT/DELETE requests are restricted exclusively to the
HRrole.
The CustomUserDetailsService is responsible for loading the AppUser from the database and mapping the role field to a SimpleGrantedAuthority (prefixed with ROLE_).
Sources: Part1/API/src/main/resources/api-spec.yaml:12-19, Part1/API/src/main/java/com/example/api/security/SecurityConfig.java:42-47, Part1/API/src/main/java/com/example/api/security/CustomUserDetailsService.java:26-34
Summary
The PasswordGenerator utility serves as a vital tool for initializing the security state of the project. By providing a direct way to generate BCrypt hashes, it enables developers to maintain a secure environment where plain-text passwords are never stored, ensuring the integrity of the administrative user base from the project's inception.
Sources: Part1/API/src/main/java/com/example/api/PasswordGenerator.java:1-12, Part1/API/src/main/java/com/example/api/config/DataLoader.java:13-17