Sunday, July 28, 2019

React with Redux authorization workflow example

This a simple React example which demonstrates how React Router Redirects (Auth) works.

Complete source code can be found in GitHub -
https://github.com/madan712/auth-workflow

Below packages are used in this example -
  • React router - React Router is used to manage navigation in react application
  • Redux - Redux is used to manage state of react applications
  • react-bootstrap - React-Bootstrap is a complete re-implementation of the Bootstrap components using React
  • react-redux-toastr - Used to show alert messages
  • Webpack - Webpack is used to create prod ready build which can be used to deployed in production
  • Babel - Babel is used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments

About the example - We have a landing page i.e public page which can be viewed by anyone. As this is a public page, user need not required to go through any sort of authorization


Our private page is a restricted page. If user clicks on private page link, user is taken to login page where user is asked to enter user name and password



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