Quick revision
The short answer
We add spring-cloud-starter-vault-config and set spring.config.import=vault://, and Spring Boot then reads secrets from Vault's KV engine at startup and makes them ordinary configuration properties, so @Value and @ConfigurationProperties work unchanged. The application authenticates with a platform method such as Kubernetes service account or AppRole rather than a static token, and KV version 2 keeps versions of each secret. This is better than application.yml or environment variables because files and images leak through repositories, logs and registries, and environment variables leak through process listings and dumps, while Vault adds access policies, audit logs and central rotation.
What to remember
In this answer, we saw that Spring Cloud Vault plugs Vault into Spring Boot through spring-cloud-starter-vault-config and spring.config.import=vault://, so KV secrets become ordinary properties read at startup. The application authenticates with a platform identity such as Kubernetes or AppRole instead of a static token, and KV v2 keeps versions of each secret. Compared with application.yml, images or environment variables, which leak through repositories, registries, process listings and dumps, Vault adds access policies, audit logs and central rotation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add spring-cloud-starter-vault-config and set spring.config.import=vault://, and Spring Boot then reads secrets from Vault's KV engine at startup and makes them ordinary configuration properties, so @Value and @ConfigurationProperties work unchanged. The application authenticates with a platform method such as Kubernetes service account or AppRole rather than a static token, and KV version 2 keeps versions of each secret. This is better than application.yml or environment variables because files and images leak through repositories, logs and registries, and environment variables leak through process listings and dumps, while Vault adds access policies, audit logs and central rotation.
Why this question is asked
Almost every service needs secrets: database passwords, API keys for payment providers, signing keys. Where those secrets live decides how far a small mistake spreads. A password committed in application.yml ends up in every clone of the repository, every CI log that prints configuration and every container image built from it. Interviewers ask this question to see whether we know how Spring Cloud Vault plugs into Boot's configuration system and why a secret store is more than just another place to keep values. In this answer, we will recap what Vault is, set up Spring Cloud Vault step by step, look at authentication and KV paths, and then compare it with files and environment variables.
1A Quick Recap of Vault
HashiCorp Vault is a server that stores and issues secrets. Clients authenticate, receive a token with policies attached, and use that token to read only the paths the policies allow. Secrets live in secrets engines. The KV engine (key-value) stores static secrets such as an API key; version 2 of KV keeps a history of versions so we can see and roll back changes. Other engines generate dynamic secrets, such as a database user created on demand.
Every request to Vault is written to its audit log, which records who read which path and when, without logging the secret values themselves.
2Setting Up Spring Cloud Vault
Spring Cloud Vault connects Vault to Spring Boot's configuration loading. Since Boot 2.4, the recommended way is the spring.config.import property, which tells Boot to load an extra property source while it builds the Environment. The Vault values then sit alongside application.yml and environment variables, and the rest of the application does not know or care where a property came from.
With this in place, a key stored in Vault under secret/orders, such as payments.api-key, becomes the property payments.api-key. Code that already uses @Value("${payments.api-key}") or a @ConfigurationProperties class keeps working without change. Check the Spring Cloud release train that matches your Boot version for exact property names.
3How the Application Proves Who It Is
The application must authenticate to Vault, and the weak option is a static token in configuration, which only moves the problem: now the token is the secret that leaks. Instead we use an authentication method that relies on an identity the platform already gives the workload.
On Kubernetes, the pod's service account token is sent to Vault, Vault checks it with the Kubernetes API, and if the service account and namespace match a configured role, it returns a short-lived Vault token. Outside Kubernetes, AppRole uses a role id and a secret id delivered by the deployment system, and cloud methods such as AWS IAM use the instance's cloud identity. In each case no long-lived secret is written into the application's configuration.
7 messages in order. Play them, or step through them one at a time.
The only credential the pod holds is the identity Kubernetes already gave it.
4Reading Secrets From KV Paths
By default, Spring Cloud Vault reads several contexts for an application: the application name and the name plus each active profile, such as secret/orders and secret/orders/prod, so profile-specific values override general ones. KV v2 stores data under secret/data/<path>, and Spring Cloud Vault handles that prefix for us. Because KV v2 keeps versions, an operator who writes a new API key can see the previous version and roll back if the new one was wrong.
Notice that the values are read at startup. If a secret changes in Vault, the running application keeps the old value until it reloads, which matters for rotation. Spring Cloud Vault can renew leases and, for dynamic secrets, refresh them, but for static KV secrets we usually plan a restart or a refresh mechanism.
5Why Not application.yml or Environment Variables?
A secret in application.yml is in Git. Removing it later does not help, because the history keeps it, and every developer laptop, CI runner and fork has a copy. A secret baked into a container image is visible to anyone who can pull the image from the registry, and images are often kept for years.
Environment variables are better than files in Git, but they still leak. On Linux they can be read from /proc/<pid>/environ by users with enough access, they appear in docker inspect and Kubernetes pod specs, crash reporters and debug endpoints sometimes dump them, and they are inherited by child processes. And they are usually set from a deployment file, which brings us back to Git or a CI variable store without audit.
Vault adds three things none of those places have: access policies, so the orders service can read only its own path; an audit log of every read; and a central place to rotate a secret once instead of editing many deployment files.
| Place | How it leaks | Audit and rotation |
|---|---|---|
application.yml in Git | Repository history, forks, CI logs | None |
| Container image | Anyone with registry pull access | None |
| Environment variable | Process info, pod specs, dumps, children | Weak |
| Vault with platform auth | Only via the app or its policy | Audit log, central rotation |
6Follow-up questions
Is the Kubernetes Secret object not enough?
Kubernetes Secrets are only base64 encoded by default and are readable by anyone with get access on secrets in the namespace; they are stored in etcd, which needs encryption at rest configured. They are a reasonable step up from Git, but they lack Vault's fine-grained audit, dynamic secrets and central rotation.
What happens if Vault is down when the service starts?
By default the import fails and the application does not start, which is usually what we want rather than starting without credentials. We can use the optional:vault:// prefix to allow startup without Vault, but then the code must cope with missing properties. Vault itself is normally run highly available.
7Conclusion
In this answer, we saw that Spring Cloud Vault plugs Vault into Spring Boot through spring-cloud-starter-vault-config and spring.config.import=vault://, so KV secrets become ordinary properties read at startup. The application authenticates with a platform identity such as Kubernetes or AppRole instead of a static token, and KV v2 keeps versions of each secret. Compared with application.yml, images or environment variables, which leak through repositories, registries, process listings and dumps, Vault adds access policies, audit logs and central rotation.
Open with
I add spring-cloud-starter-vault-config and set spring.config.import to vault://. During startup, Boot then loads Vault as another property source, so a key in Vault such as payments.api-key becomes a normal property and @Value or @ConfigurationProperties work unchanged.
Then, if they want more
By default it reads the KV paths for the application name and for each active profile, like secret/orders and secret/orders/prod, and with KV version 2 every secret has a version history. For authentication I avoid a static Vault token, because that just becomes the secret that leaks.
On Kubernetes the pod sends its service account token, Vault validates it with the Kubernetes API and returns a short-lived token with a policy that allows only that service's paths; elsewhere I would use AppRole or the cloud's IAM method. Why is this better?
A secret in application.yml lives in Git history forever and spreads to every clone and CI log, and one in an image is visible to anyone who can pull it. Environment variables can be read from process information and pod specs, and they show up in dumps and are inherited by child processes.
Vault gives me policies so each service reads only its own secrets, an audit log of every read, and one place to rotate a value. One thing to remember is that the values are read at startup, so rotation needs a refresh or a restart.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does a Spring Boot application read secrets from HashiCorp Vault with Spring Cloud...": https://scrivio-interview-library.netlify.app/spring-boot/secrets-and-crypto/fc43fee4e5/