Secure Configuration: Storing Secrets and Environment Variables in Micronaut

Modern applications rely heavily on configuration values—API keys, database credentials, JWT secrets, OAuth tokens, and more. But hardcoding these values or storing them in plain files is a serious security risk. That’s why Secure Configuration in Micronaut is a fundamental skill for developers building cloud-native or microservice-based applications.

In this post, we’ll explore secure techniques to manage secrets and environment variables in Micronaut, using the package com.kscodes.micronaut.security. We’ll cover .env, system environment variables, secure property sources, and secret managers like HashiCorp Vault and AWS Secrets Manager.

Secure Configuration in Micronaut

📦 Why Secure Configuration Matters

  • Prevents credential leaks in code repositories.
  • Isolates environment-specific values (dev, staging, production).
  • Enables secrets rotation without code changes.
  • Ensures compliance with security and auditing standards.

🔧 Default Config Hierarchy in Micronaut

Micronaut supports multiple configuration sources with the following priority:

  1. System Properties (-Dproperty=value)
  2. Environment Variables (export ENV_VAR=value)
  3. application.yml / application.properties
  4. External Config Files (--config.location)
  5. Custom PropertySource Loaders (e.g., Vault, AWS)

Micronaut will automatically merge and prioritize based on this order.

🔑 Using Environment Variables

Step 1: Define in Shell or .env (local dev)

Step 2: Reference in application.yml

${JWT_SECRET:default-secret} – Uses env var or falls back to default.

📁 External Configuration with .env (Dev Only)

Micronaut doesn’t load .env by default like Spring Boot, so use a plugin:

  1. Add dotenv library (Gradle):
  1. Load .env in main():

🔒 Using HashiCorp Vault with Micronaut

Step 1: Add Vault Dependency

Step 2: Vault Configuration (bootstrap.yml)

Step 3: Store Secret in Vault

Micronaut will auto-map it using the config prefix.

☁️ Using AWS Secrets Manager (Alternative)

Step 1: Add Dependency

Step 2: Configure AWS IAM Role or Access Keys

Micronaut will fetch values based on secret name and inject into config.

👨‍💻 Example Usage in a Micronaut Class

File: com.kscodes.micronaut.security.SecureService.java

🛡️ Best Practices

  • Never commit secrets to version control.
  • Use .gitignore to ignore .env or bootstrap.yml in local dev.
  • Rotate secrets periodically.
  • Limit access to config files using file permissions.
  • Use separate secrets for each environment.

🧪 Test Secure Configuration Locally

Make sure the app loads secrets via logs or endpoint inspection.

📚 External References

✅ Conclusion

Secure Configuration in Micronaut is a critical pillar of secure application development. Whether you’re building small services or large cloud systems, managing secrets through environment variables, Vault, or AWS Secrets Manager helps protect sensitive data and makes your application more secure and maintainable.

This post used the com.kscodes.micronaut.security package for clean examples that you can extend across multiple modules or environments.