Spring Boot how to hide passwords in properties file
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Keeping plaintext passwords in application.properties is a common security mistake that often leads to leaks through source control, logs, or build artifacts. Spring Boot supports safer patterns that separate secret values from committed configuration. The goal is not only hiding secrets, but enabling secure rotation and controlled access.
Use Placeholders and Runtime Injection
A simple baseline is placeholder-based config with environment injection.
Set the secret at runtime:
This removes plaintext secrets from repository files and works in local and CI environments.
Keep Profiles Non-Sensitive
Avoid storing secrets in profile-specific files such as application-prod.properties. Keep all profiles referencing the same external secret source.
This prevents one profile from becoming an accidental security exception.
Use Secret Managers in Production
Environment variables are better than plaintext commits, but production systems usually need audit trails and rotation workflows.
Common options:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
Spring-style Vault import example:
This keeps credentials out of static property files.
Encrypted Property Values as Transitional Option
If constraints require storing encrypted strings in config, keep decryptor key external and tightly controlled.
Runtime key example:
Encrypted values in Git still require strict key management. Encryption is not a replacement for secret governance.
Rotation and Revocation Workflow
A secure setup must support lifecycle operations:
- rotate secrets regularly
- update runtime source automatically
- restart or refresh app safely
- revoke compromised credentials quickly
Short-lived credentials reduce exposure window compared with static passwords.
Prevent Secret Leakage in Logs
Even externalized secrets can leak if you log full URLs or environment dumps. Log non-sensitive identifiers only.
Also configure CI systems to mask secrets in build output and test traces.
Practical Local Development Pattern
Use a local .env or shell profile that is ignored by source control and populated per developer machine. Document expected variable names clearly so onboarding does not lead to temporary plaintext commits.
CI and Deployment Guardrails
Add automated checks that block commits containing likely secrets and validate that required environment variables are present in deployment manifests. Secret scanning in pull requests catches accidental leaks early, while startup validation prevents deployments with missing secret bindings.
Security improves when prevention and detection are both present across developer workflow and runtime platform. Keep emergency secret-rotation runbooks tested periodically so credential replacement can be executed quickly when exposure is suspected. Run post-rotation validation checks automatically to confirm applications have reloaded the new values successfully. Track rotation timestamps centrally for compliance and audit reporting.
Common Pitfalls
- Committing plaintext passwords in one profile file while others are externalized.
- Storing encrypted values and decryptor keys in the same repository.
- Assuming environment variables are secure without controlling process access.
- Logging full JDBC URLs including credentials.
- Ignoring rotation and keeping long-lived static passwords.
Summary
- Never store plaintext passwords in committed Spring property files.
- Use placeholders with runtime injection as a minimum baseline.
- Prefer managed secret stores for production deployments.
- Treat encryption as supplemental, not complete secret strategy.
- Combine storage controls with rotation, logging hygiene, and incident response.
Related reading
- Spring Boot How to specify the PasswordEncoder?
- Spring boot Kafka class deserialization - not in the trusted package
- Spring Boot MSSQL Kerberos Authentication
- Spring Boot Oauth2 client credentials
- Spring boot How to read resource from classpath in unit test
- Spring Boot how to use multiple yml files
- Spring Boot project shows the Login page
- Spring Boot redirect HTTP to HTTPS

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.