How do you hide API Key when using Spring boot with Maven?

Viewed 781

I have seen examples with Gradle, where API key is stored in gradle.properties file. After that modifying the build.gradle file and then using a variable for it in Java app.

How can I do something similar in a Maven Project?

1 Answers

You can configure your api key to don't be pushed to your git remote repository, and then use it on your project using the @Value annotation.

You can configure the file containing your api key as "application-dev.properties" so it can't be pushed to the remote repository.

.gitignore

application-dev.properties

It can be created under

/resources/application-dev.properties

You can define your api key there

application-dev.properties

apiKey=your_key

And then in some class that you want to use it

SomeClass.java

public class SomeClass {

    @Value("${apiKey}")
    private String apiKey;

}

Then you can use this variable apiKey as your api key, hiding the actual value.

Related