Saturday, June 29, 2019

Spring Boot + Jasypt example to encrypt database password in property file

In this example we will see how to encrypted database password in property file (application.properties or application.yml). We will use Jasypt library for this purpose.

Jasypt (Java Simplified Encryption) is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

To know more about Jasypt, click here. Go to Jasypt website and download the latest version of jasypt client. I am using jasypt-1.9.2-dist.zip. Once downloaded extract the zip file and go to folder /jasypt-1.9.2/bin and execute the below command. Here input is your password or any other text that you want to encrypt and password is the secret key used by Jasypt to encode and decode the input.

encrypt.bat input="dummy_password" password="SECRET_KEY"


To know about encrypting from the command line using Jasypt CLI Tools. click here.

Add below maven dependency in your pom.xml file

<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

application.yml
Note - the encrypted password in parenthesis with keyword ENC 


There are various options by which you can feed the SECRET_KEY to your application

- From java code you can set system property jasypt.encryptor.password as shown below. You can do this in a separate secure JAR file.

System.setProperty("jasypt.encryptor.password", "SECRET_KEY");


- From command line you can pass system properties

$ Java -Djasypt.encryptor.password=SECRET_KEY Application

My Sql + Spring Boot JPA - One to many, many to one example

This is a simple spring boot JPA (hibernate) example. It will demonstrate how one to many, many to one mapping can be done

Below are the tables created in MySQL

create table department
(
 department_id int not null auto_increment,primary key (department_id),
 department_name  varchar(50) not null
);


create table employee
(
 employee_id int not null auto_increment, primary key (employee_id),
 employee_name varchar(50) not null, 
 department_id int not null, foreign key (department_id) references department(department_id)
);

pom.xml


application.yml


Friday, June 28, 2019

Java - Export database table to CSV file

This is a sample springboot application with bare minimum code to export database tables to CSV file

pom.xml

application.yml

Application.java

Python - Export database table to csv file


Java - Export database tables to an excel file

This is a sample springboot application with bare minimum code to export database tables to an excel file

pom.xml

application.yml

Application.java