Guide

How to store API keys in Java (Spring Boot) safely

Spring Boot reads configuration from environment variables, properties files and other sources through its Environment. Reference a secret with @Value("${STRIPE_SECRET_KEY}") or bind a group of values to a @ConfigurationProperties class, and supply the real value from the environment or mounted files, not from a committed application.properties.

Read the key from the environment

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PaymentClient {
    private final String apiKey;

    public PaymentClient(@Value("${STRIPE_SECRET_KEY}") String apiKey) {
        this.apiKey = apiKey; // fails at startup if the variable is missing
    }
}

Local development

In development you can export variables in your shell or IDE run configuration, or use a git-ignored application-local.properties activated with spring.profiles.active=local. Do not put real values in application.properties, which is normally committed. Spring Boot also supports spring.config.import=optional:file:./.env[.properties] for an env-style file, which you should keep out of Git.

Production

Provide secrets as environment variables or as files mounted by Kubernetes or Docker and import them with spring.config.import=configtree:/run/secrets/, where each file name becomes a property. Spring Cloud and cloud vendors also offer integrations with secret managers. Keep Actuator's env and configprops endpoints disabled or locked down; Boot sanitises known sensitive keys by default but you should not rely on that alone.

Mistakes to avoid

  • Committing application.properties or application.yml with literal passwords or API keys.
  • Exposing Actuator endpoints such as /actuator/env or /actuator/heapdump publicly, which can reveal configuration values.
  • Logging @ConfigurationProperties objects with Lombok's @ToString or records, which prints secret fields.
  • Using the same credentials for all profiles, so a leaked development key opens production.

When an AI agent works on your Java (Spring Boot) project

Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.

Frequently asked questions

How do I keep passwords out of application.properties?

Reference them as placeholders such as ${DB_PASSWORD} and supply the value from an environment variable or a mounted secret, imported with spring.config.import=configtree:.

Is Spring Boot's Actuator safe for secrets?

Only if you restrict it. Expose the minimum endpoints, secure them, and keep env and heapdump off public networks, since they can reveal configuration.

Should I keep API keys in a .env file for Java (Spring Boot)?

A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.

Other languages and frameworks

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.