Using jasypt-spring-boot when deploying to Apache Tomcat - spring

I'm trying to use the jasypt-spring-boot and deploy it to a Tomcat server as war.
How to pass the encryptor password, in this case, to ensure the encrypted values could be read?
All the provided example are about running a jar file or a Spring Boot app as follows:
java -Djasypt.encryptor.password={my-password-to-decrypt} -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar
May be add some settings to catalina.properties file in the Tomcat conf folder as we do for example when defining active profile?

I figured out how to achieve that:
create a setenv.sh file in CATALINE_HOME/bin folder
add the following entry to set the environment variable on the Tomcat startup:
export JASYPT_ENCRYPTOR_PASSWORD=your-password
save and restart Tomcat.

Related

Spring Boot - Externalize Database Settings

I have a JAVA project developed with Spring Boot.
The database settings are in the default resources/application.properties file.
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#//172.********:1521/BDHML
spring.datasource.username=********
spring.datasource.password=********
The application will run through the command:
java -jar **CONFIGURAÇÃO** app-cosolidar.jar
I need to put the database settings in a .properties file external to the project.
How can I do this?
What should the .properties file look like?
Should I change any .java files?
What setting should I put in java -jar?
Regards, Diego
You can provide command line arguments while running jar.
java -jar app.jar --spring.config.location=file://<path>/application.properties
You can also pass a folder location where the application will search for the files.
java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file://<path to config folder>
Refer this link for more understanding. https://www.baeldung.com/spring-properties-file-outside-jar
You would be able to launch your spring boot application with the external properties file path as follows:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

how to set profile-specific applicaiton.properties(spring boot) outside war(jboss)?

I have spring boot application that I am running in jboss(instead of tomcat).
I am using spring profile for loading environment specific application.properties.
Profile specific application{env}.properties is working fine when placed under "src/main/resources/" however, it is not working when placed externally.
I tried setting property in jboss standalone.xml but application fails to start in jboss.
<property name="spring.config.location" value="C:/Dev/config/rt" />
Please suggest how to load the environment specific application.properties files that are not placed inside the war file.
I was expecting spring to pick the profile specific file from the folder but looks like that's not the case.
It appears that spring.config.location needs to have the absolute file location instead of folder location. so, updated spring.config.location in standalone.xml and it worked :
<property name="spring.config.location" value="C:/Dev/config/rt/application-dev.properties" />
You can provide externalized configuration file using below command when you are initially starting the application,
java -jar <your-jar-name.jar> --spring.config.location=<path><external property>
example :
java -jar mySample.jar --spring.config.location=./application-external.properties
read more
Generally it was picked very easily when it's placed under the 'src/main/resources' folder. suppose you have to different files for profiles like - application-dev.properties and application-prod.properties, you need to set only the current working profile in the application.properties like
spring.profiles.active=dev
and it will be picked easily. If it's doesn't you need to create a workaround by creating a new bat or sh file like run.bat and run.sh in the bin folder of the jboss and pass the file location like
--spring.config.location=
The complete command to be added in the bat/sh file will be
java -jar appName.jar --spring.config.name=application-dev --spring.config.location=c:/Dev/application-dev.properties

Logback external configuration on Tomcat

I have Spring Boot application deployed as WAR file on standalone Apache Tomcat.
In order to configure Logback on Windows machine I've created setenv.bat file with the following content:
set logging.config=<absolute path to logback.xml>
I was trying the same for Tomcat deployed on Mac, but the following setenv.sh doesn't work:
logging.config=<absolute path to logback.xml>
I've also tried set logging.config variable using export command:
export logging.config=<absolute path to logback.xml>
but result is the same - log files aren't created.
Any ideas what is wrong with my configuration?
I hope you already have found your answer somewhere. But what I have done is that in conf/catalina.properties, point to the logback config file as follows:
logging.config=${catalina.base}/conf/logback-spring.xml
I don't do anything in setenv.bat.

Build Spring project for run on another system

I Successfully create a spring boot project on my own local system. I want to build a jar file so I can install it on remote server. so I had to configure server address and mySql address of remote server but I can not Build and it have many errors, and they all right cause my system can not see the remote server address and database.
this is my .properties file:
spring.datasource.url=jdbc:mysql://localhost:8081/aths
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
server.address=192.168.24.250
server.port=8080
how can handle it for running on another configurations? ( another IP, datasource, and ...)
Am I doing it right or not? thanks
You can use spring profiles here :
Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and application-prod.properties for production put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.
and at the end, you will run your jar file with command
java -jar -Dspring.profiles.active=prod application.jar
You can have different application.properties within your resources folder and use spring profiles for example application-{profile}.properties and run the application with the specified profile. However this still limits the configuration items to what has been hard coded within the properties files. When running the application, if it was to be distributed to other people, where non of the profiles are supported you can provide a properties file at start up.
So in the same directory for example as the .jar file create a file named application.properties with empty place holders for all the variables required for the application so the admin can enter the details correct for them. Then they will be required to start the application with the following command
java -jar 'applicaitonname.jar -Dspring.config.name="file:/path/to/application.properties"
Or springboot will load properties from application.properties files in the following locations:
A /config subdirectory of the current directory.
The current directory
Failing that the default application.properties from the resources folder will be loaded.

How to change crashHub port in Springboot

I am using Springboot in my project, i start an embedded Springboot application inside my App. My app is already having a crashHub which is conflicting with springBoot's crashHub, so how i can change the springBoot's crashHub port, I don't want to do it through a XML File.
There's an extensive list of configuration properties in Spring Boot's documentation, including those for configuring the ports used by the remote shell: shell.ssh.port and shell.telnet.port.
You should configure one or both of these properties in your application.properties file in src/main/resources.
Additionally, you can do it by specifying parameter to java:
-Dshell.ssh.port=<your-port>
Or command line argument for you application:
--shell.ssh.port=<your-port>

Resources