Home > Net >  How to provide spring boot application.yml property values coming from files
How to provide spring boot application.yml property values coming from files

Time:04-02

Is there a way for providing spring boot properties value text from file? I tried something like below but it's not working.

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-secret: classpath:./secrets/google-client-secret.txt
            client-id: classpath:./secrets/google-client-id.txt

The secrets folder is created inside resources folder

CodePudding user response:

I'm afraid that it is not possible to do that.

What I recommend you tho is to use environment variables to achieve that:

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-secret: ${GOOGLE_CLIENT_SECRET}
            client-id: ${GOOGLE_CLIENT_ID}

This way the secrets will be tied to the environment and not into the yml file.

CodePudding user response:

The way you're implementing this is not possible. There are several other ways to implement externalized configuration though.

Using environment variables

In your IDE you can configure environment variables. If the names of the environment variables match the name of the property, you can remove the parameters from application.yml. For example:

SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_GOOGLE_CLIENT_SECRET=actual-secret
SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_GOOGLE_CLIENT_ID=actual-id

You can also name your environment variables differently. For example:

GOOGLE_CLIENT_SECRET=actual-secret
GOOGLE_CLIENT_ID=actual-id

In that case, you have to change your application.yml to this:

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-secret: ${GOOGLE_CLIENT_SECRET}
            client-id: ${GOOGLE_CLIENT_ID}

Using a separate YAML file

Another possibility is to create a file called secrets.yml, make it contain your actual values and put it in .gitignore. This solution is the closest to what you want.

For example, in secrets.yml you can put:

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-secret: actual-secret
            client-id: actual-id

Now you can load it by using the --spring.config.additional-location VM argument (also configurable in your IDE):

java -jar myapp.jar --spring.config.additional-location=classpath:secrets.yml

Using VM arguments

Speaking of VM arguments, you can also pass the properties themselves as VM arguments.

For example:

java \
  -jar myapp.jar \
  -Dspring.security.oauth2.client.registration.google.client-id=actual-id \
  -Dspring.security.oauth2.client.registration.google.client-secret=actual-secret

Like with the environment variables, you can use shorter VM arguments and reference them in application.yml. For example by using -Dgoogle.client-id=actual-id and putting ${google.client-id} in your application.yml.


There are several other ways of providing externalized configuration, so I suggest you read the documentation I linked earlier.

  • Related