Quick revision
The short answer
Spring Vault gives us VaultTemplate to read and write secrets in HashiCorp Vault, including the KV v1 and v2 engines, transit and PKI, and Spring Cloud Vault adds a config data source so that secrets appear as normal Spring properties through spring.config.import=vault://. On Kubernetes we use Vault's Kubernetes auth method, where the pod's service account token is exchanged for a short-lived Vault token bound to a role, instead of a static or root token in configuration. Leases and tokens are renewed by Spring Vault's session management, but secrets read at startup do not refresh by themselves.
What to remember
In this answer, we saw that Spring Vault provides VaultTemplate for KV, transit and PKI, while Spring Cloud Vault turns secrets into properties through spring.config.import=vault://. On Kubernetes, the Kubernetes auth method exchanges the pod's service account token for a short-lived Vault token bound to a role, so no static or root token is needed, and AppRole fills that role elsewhere. Spring Vault renews tokens and leases, but secrets read at startup do not refresh by themselves, and cloud secret managers or the External Secrets Operator are the main alternatives.
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.
Spring Vault gives us VaultTemplate to read and write secrets in HashiCorp Vault, including the KV v1 and v2 engines, transit and PKI, and Spring Cloud Vault adds a config data source so that secrets appear as normal Spring properties through spring.config.import=vault://. On Kubernetes we use Vault's Kubernetes auth method, where the pod's service account token is exchanged for a short-lived Vault token bound to a role, instead of a static or root token in configuration. Leases and tokens are renewed by Spring Vault's session management, but secrets read at startup do not refresh by themselves.
Why this question is asked
Passwords and API keys in property files end up in Git history, container images and screenshots. A secret store such as Vault fixes that, but only if the application can authenticate to Vault without another long-lived secret, which is the real puzzle. Interviewers ask this question to see whether we know the two Spring projects, how secrets become properties, and which authentication method solves the bootstrap problem on Kubernetes. In this answer, we will recap Vault, read secrets both ways, authenticate with Kubernetes auth, look at renewal and refresh, and compare alternatives.
1A Quick Recap of Vault
Vault is a server that stores secrets and controls who can read them. Secrets live in secrets engines mounted at paths: the KV engine stores key-value pairs (version 2 keeps a history of versions), the transit engine encrypts data without storing it, the PKI engine issues certificates, and the database engine creates database users on demand.
Every request needs a Vault token, and every token has policies that say which paths it may read or write. Tokens and dynamic secrets have leases, a time to live after which they expire unless renewed. Getting the first token, without already holding a secret, is what auth methods are for.
2Reading Secrets as Properties With Spring Cloud Vault
Spring Cloud Vault plugs into Spring Boot's config data mechanism. We import Vault as a property source, and the secrets under the application's path become properties like any other.
Notice that the code has no Vault types at all; it just reads a property. That keeps secrets management swappable, which matters later.
3Reading and Writing With VaultTemplate
For anything beyond properties, such as writing secrets, encrypting with transit or issuing certificates, Spring Vault's VaultTemplate is the API. Spring Cloud Vault configures one for us, already authenticated.
4Authenticating on Kubernetes Without a Static Token
The tempting shortcut is to put a Vault token in an environment variable. But that token is itself a long-lived secret, often with broad policies, and if it is a root token it can do anything. Leaking it gives an attacker every secret it can read. So static and root tokens in configuration are ruled out.
Kubernetes already gives every pod an identity: a service account token mounted into the container, signed by the cluster. Vault's Kubernetes auth method accepts that token, asks the Kubernetes API whether it is valid and which service account it belongs to, and if the service account matches a configured Vault role, returns a short-lived Vault token with that role's policies. The pod never holds a long-lived secret.
7 messages in order. Play them, or step through them one at a time.
Outside Kubernetes, AppRole is the usual choice: a role id that identifies the application plus a secret id delivered at deploy time with a short lifetime and limited uses. Cloud platforms also offer auth methods based on their own identities, such as AWS IAM.
5Renewal, Leases and Refresh
Spring Vault's session management keeps the Vault token alive: it renews it before it expires and logs in again when renewal is no longer possible. Leased secrets, such as dynamic database credentials, are handled by a lease container that renews them in the background.
But there is a catch for static KV secrets read as properties. They are read once, at startup, and the value in the Environment does not change if someone updates it in Vault. To pick up a rotated KV secret we need refresh support, for example a scope that rebuilds beans on refresh, or simply a rolling restart of the pods after rotation, which is often the most predictable choice. So rotation must be planned together with how the application reloads.
6Alternatives to Vault
Cloud secret managers, such as AWS Secrets Manager, Azure Key Vault or Google Secret Manager, offer similar storage with the cloud's own identity, and Spring Cloud AWS and Spring Cloud Azure have property-source integrations for them. Another common pattern is the External Secrets Operator on Kubernetes, which copies secrets from any of these stores into Kubernetes secrets that the pod reads as files or environment variables, so the application needs no secret-store client at all. The choice depends on whether we need Vault's dynamic secrets and transit features or just safe storage.
7Follow-up questions
How do you use the transit engine to encrypt a field?
We create a key in transit, for example orders-key, and give the application a policy that allows only encrypt and decrypt on it. Before saving, the application calls vaultTemplate.opsForTransit().encrypt("orders-key", value) and stores the returned ciphertext, which starts with vault:v1:, in the database column; reading calls decrypt. The key never leaves Vault, so a database dump alone reveals nothing. When the key is rotated, old ciphertext still decrypts, and a rewrap call upgrades it to the new key version without exposing the plaintext.
Is a Kubernetes secret enough on its own?
Kubernetes secrets are only base64-encoded by default and readable by anyone with access to the namespace, so they need encryption at rest and strict access control. They are fine as a delivery mechanism, as with the External Secrets Operator, but they are not a secret store with audit and dynamic secrets.
What happens if Vault is unreachable at startup?
With fail-fast enabled, the application refuses to start, which is safer than starting without secrets; Kubernetes keeps retrying the pod.
8Conclusion
In this answer, we saw that Spring Vault provides VaultTemplate for KV, transit and PKI, while Spring Cloud Vault turns secrets into properties through spring.config.import=vault://. On Kubernetes, the Kubernetes auth method exchanges the pod's service account token for a short-lived Vault token bound to a role, so no static or root token is needed, and AppRole fills that role elsewhere. Spring Vault renews tokens and leases, but secrets read at startup do not refresh by themselves, and cloud secret managers or the External Secrets Operator are the main alternatives.
Open with
There are two Spring projects. Spring Vault is the client library: VaultTemplate lets me read and write KV secrets, version 1 or 2, encrypt with the transit engine and issue certificates with PKI.
Then, if they want more
Spring Cloud Vault builds on it and adds a config data source, so with spring.config.import set to vault colon slash slash, the secrets under the application's path, for example secret/data/orders, show up as normal Spring properties and my code just uses @Value or configuration properties without any Vault types.
On Kubernetes I would use the Kubernetes auth method instead of a static token. The pod already has a service account token; Spring Cloud Vault sends it to Vault with a role name, Vault validates it with the Kubernetes API, checks that the service account and namespace match the role, and returns a short-lived Vault token with that role's policies.
So there is no long-lived secret in the configuration, and certainly no root token. Outside Kubernetes I would use AppRole or the cloud's identity auth. Spring Vault's session management renews the token and leases in the background, but static secrets read at startup do not update on their own, so rotation needs refresh support or a rolling restart.
Alternatives are cloud secret managers or Kubernetes secrets fed by the External Secrets Operator.
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 Boot 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 Vault...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/7f5edf9425/