Externalize password in Spring Boot properties file - spring-boot

i have application.properties file
which has
spring.profiles.active=local
and i have application-local.properties which has many fields including
api.password = password123
As you can see i have hard coded password123 in properties file.
Suppose i am on windows and i have app.properties file on windows which has
api.password = password123
And i want to read api.password in spring boot properties file through app.properties
How can i achieve it ?

Startup parameters
If you want to read properties from a non-standard location, you can start your application with --spring.config.location or with --spring.config.additional-location parameters.
$ java -jar app.jar --spring.config.location=file:./app.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
#PropertySource
If you don't control startup parameters, you can use #PropertySource annotation.
Simply annotate your main class (or any other configuration):
#PropertySource("file:.app.properties")
You can set ignoreResourceNotFound=true, so the application will start, even if the file is not there.
https://docs.spring.io/spring/docs/5.1.9.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html
Read the documentation
There are literally 17 ways to pass properties to a Spring-Boot application.
I suggest, you get familiar with the convention, as it is crucial to understand which file takes a precedence:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config

Probably you can use --spring.config.location like below
$ java -jar myApp.jar --spring.config.location=file:/directoryof file/app.properties

i used jasypt : https://github.com/ulisesbocchio/jasypt-spring-boot
to encrypt the password, then you supply a decryption string (jasypt.encryptor.password) as startup parameter. That way no useabel password is in the configuration and on the git-repos.....

Related

spring-boot - how to specify path of application.properties in envrionment or system properties?

If I want to run spring boot application, and want to use difference application.properties in difference path (other than using profile)
How can I specify path for application.properties?
If you want to use difference application.properties in difference path, use this command to start the jar file
nohup java -jar project.jar --spring.config.location=file://{file-path}/application.properties
environment variable SPRING_CONFIG_LOCATION can be also used.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
#PropertySource annotation is used to provide properties file into the environment and it is used with #Configuration classes.
#PropertySource({ "classpath:config.properties" })
use bellow code above main class:
#PropertySource(value = {"file:///${HOMEDIR}/application.properties"})

Spring Boot Reading Properties Files Based on classpath arg

I have created a standalone boot.jar that I need to start integrating into our higher environments.
Each environment has a property file that contains database specific connection information. Since this does not live in my boot jar, I would like to somehow add the path to this database.properties file and then read the entries based on key. Used to create a bean like so:
<bean id="propertyLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:application.properties</value>
</property>
but with boot, I am not sure how to do this: But I want to point to this below property example and pull out my values and somehow populate the application.properties values that I hardcoded in dev.
server:/config/database.properties
jdbc.username=TEST
jdbc.password=CHANGEME
Updating my application.properites:
spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'
Something like that do I can parameterize my application.properties file.
SpringBoot offers profiles, which basically allows you to have separate application.properties file for each environment.
You can have something like this:
public interface DataSourceConfig {}
#Component
#Profile("dev")
public DevDataSourceConfig implements DataSourceConfig{}
#Component
#Profile("prod")
public ProdDataSourceConfig implements DataSourceConfig{}
If you have the spring profile "dev" set as active, only the DevDataSourceConfig bean will be instantiated and in Spring Environment the properties that will be injected, will be read from the application-dev.properties file.
Similarly when you have the "prod" profile activated, only the ProdDataSourceConfig will be instantiated and the properties will be loaded from application-prod.properties file.
This allows you to have:
---
application-dev.properties
spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'
---
application-prod.properties
spring.datasource.username='PROD_jdbc.username'
spring.datasource.password='PROD_jdbc.password'
If you want to load the configuration from a custom location on the file system - you can check how to pass the location with command line arguments (docs)
Example:
java -jar boot.jar --spring.config.location=classpath:/database.properties
you already told you can not have property files inside your jar, still there are multiple options.
1> passing a property file for respective env.
java -jar myproject.jar --spring.config.location=classpath:/database.properties
2> pass properties while calling the jar
java -jar app.jar --spring.datasource.username="jdbc.username" --spring.datasource.password="jdbc.password"
Read a lot of other options here `
I would go with option 1, because passing credentials is never advisable in arguements.

Spring boot - Setting configration property thru program arguments

I have a spring boot application and the setting file with the below annotation.
#ConfigurationProperties("test.prop")
public class TestPropSettings {
private String name;
}
The following property in the application.properties set this value.
test.prop.name=XYZ
But, I would like to pass thru program arguments without having the property file.
Tried with,
-Dtest.prop.name=XYZ in eclipse program arguments. But, it does not work. Is there any other way?
Thanks
If you start your jar directly, you can override the property like this:
java -jar your-app.jar --test.prop.name=XYZ
In Eclipse you also need to pass --test.prop.name=XYZ in you program arguments.

Running bootRun and use a different application.properties file

I'd like to be able to run bootRun for spring boot, but use a different application.properties file besides the one in src/main/resources/. Is that possible? I'd prefer to not overwrite the file in src/main/resources/, as it would dirty the file.
Is this possible?
You can use profile based configuration selection.
Just set a system environment property:
spring.profiles.active=dev
And now provide application-dev.properties in application resources(src/main/resources/)
By this way you can use different properties for different environment.
If you want to provide files at a different location the use this environment property
spring.config.location=<path>
If you wish to use different name then application in property file name the use this environment property:
spring.config.name=<new_name>
For more info check this link:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
Here is what my bootRun task looks like that does this
bootRun {
systemProperty "spring.config.location", "file:$projectDir/spring-config/"
main = springBootAppClass
}
Which specifies to use the spring-config directory in the project's root folder.
If using in conjunction with #PropertySource it looks like this
#PropertySource("${spring.config.location}/persistence.properties")
Try this command,
./gradlew bootRun --args='--spring.profiles.active=dev'
I too was looking for the solution, found it in official documentation
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#running-your-application
I got it to work like this:
tasks.named("bootRun") {
systemProperty "spring.config.location", "file:$projectDir/myConfigFolder"
mainClass = "my.project.MyMainClass"
}

Extern log4.properties file with Spring Boot

I am running my app with executable jar.
I have log4j.properties inside /resources folder
In prod I would like to override it and have it within external dir
How I could do that using Spring-Boot?
-Dlogging.config=/path/to/log4j.properties
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
If you don't like to add command line arguments, you can make additional application.properties on the directory where you start the application. like:
# log4j configuration for product
logging.config=log4j-prod.properties
Then, this application.properties will override the /resources/application.properties and, the log4j-prod.properties will be used on the product environment. Please read more details on:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
try this:
java -Dlog4j.configuration=file:/log4j.properties -jar XX.jar
the -D configuration before the -jar configuration.
work for me.
-Dlog4j.configuration=file:/path/to/log4j.properties to the command line works in Spring Boot 1.5.6. Don't forget file: .

Resources