Reputation: 33
my spring boot project using the application.yml as the configuration, and I don't want to write the DB credentials in it, as the yml file is part of the source code which will be pushed into a public code repository. So I need to put the credentials into the properites file and the put the properties file somewhere on the application host server.
So, is it possbile to externalize the expression within application.yml into a property file?
e.g. sprint boot application with the the application.yml
spring:
datasource:
jdbc-url: ${url}
username: ${user}
password: ${pwd}
then we can put the credentials into app.properties as below, and put this file into the the host server.
url="jdbc:xxxx"
user="username"
pwd="password"
Upvotes: 3
Views: 3052
Reputation: 2340
This is very common concerns among all developers, how to secure database credentials in order to prevent any misuse, but trust me keeping credential in separate file won't add much value to situation.
If someone is capable of accessing you application.yml then once can also access you property file residing in separate folder.
Having understood your problem, that you need to push your code in public repository, you must protect what is yours. I have few inputs.
- you don't need separate property file and respective variable in your application.yml, you can provide your values and you can always override them outside your application during application start up.
for Example
spring:
datasource:
jdbc-url: myFakeURL
username: MyFakeUserName
password: myFakePAssword
once you have defined them, you can override them like
java -jar -Dspring.datasource.jdbc-url=myRealURL -Dspring.datasource.jdbc-username=myRealUserName -Dspring.datasource.password=myRealpassword myApplication.jar
Remember: this is one way, there are another ways too
You can create you shell script/batch file (containing these property) and execute then before starting your application
With spring boot there are few things which you MUST consider doing i.e. encrypting your property values. Spring has beautiful support with Jasypt for encrypted properties and trust me you will have to almost no effort
you can also explore Managing Secrets with Vault
Upvotes: 1
Reputation: 12953
You can do that by calling the file application.properties
.
But keep in mind the properties names should be similar to the yaml structure:
spring.datasource.url="jdbc:xxxx"
spring.datasource.user="username"
spring.datasource.pwd="password"
Upvotes: 1