Search code examples
javaspringspring-bootsecuritykey

Using a secrets.yml for API keys in Spring Boot


I have a program using Stripe API. I am trying to setup a secrets.yml file that will be in the .gitignore. Here's my current attempt:

secrets.yml:

stripe:
 STRIPE_PUBLIC_KEY: key1
 STRIPE_SECRET_KEY: key2

The relevant application.properties:

spring.profiles.include=secrets
spring.config.additional-location=classpath:secrets.yml

Both files are in src/main/resource/

This is the Java code:

@Value("${STRIPE_SECRET_KEY}")
private String secretKey;

@Value("${STRIPE_PUBLIC_KEY}")
private String stripePublicKey;

The program works correctly when the keys are inside application.properties.

EDIT:

When I use a non-top level yml, it still fails. secrets.yml:

STRIPE_PUBLIC_KEY: key1
STRIPE_SECRET_KEY: key2

Solution

  • Instead of spring.config.additional-location=classpath:secrets.yml use spring.config.import=optional:secrets.yml

    also, remove stripe parent element in your secrets.yml if you want to access the values as you specified.