Sunday, July 28, 2019

React with Redux authorization workflow example (react-router-dom v6)

This a simple React example which demonstrates how to restrict access to routes to authenticated users.

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-router-dom v6 - React routing library
  • 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

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

Sunday, June 23, 2019

Python - Connect mysql

Python needs a MySQL driver to access the MySQL database. PIP can be used to install "MySQL Connector". PIP is most likely already installed in your Python environment. Go to command prompt and execute below command

C:\> python -m pip install mysql-connector



test-mysql.sql

Sunday, June 16, 2019

Python - Read and write a text file

After coding in Java for nearly 10 years, lately i started learning Python programming and let me tell you it quickly grabbed my interest. So here is the first code for reading and writing a text file using Python. More codes will come along.

read_write_file.py

Saturday, June 1, 2019

Example with React, Redux and Axios API

In previous article we have seen a simple react with redux example. In this article we will add Axios api to it. Axios is used for Promise based HTTP client for the browser and node.js. To know more about axios package, click here.

In this example we will get input name from user and hit a hello world rest endpoint. For running this example you may need to install below packages -
  • redux
  • react-redux
  • axios
  • redux-thunk


App.js

Sunday, March 31, 2019

Simple React + Redux example

In the previous article we have seen Spring boot with react example. In this example we will see how to configure React with Redux. Now what is all this fuss about Redux?


In simple term Redux is used to manage state of your application. As your application starts growing it gets difficult to manage state of your application. This is when Redux comes to help you.

Redux data flow



Lets see a simple counter example. Assuming npm is already installed in your machine. If not, install npm.

Saturday, March 23, 2019

Spring Boot + ReactJS hello world example

Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring. To know more about spring boot, click here.

ReactJS is a JavaScript library used for building user interfaces. To know more about react, click here.

In this example spring boot will exposed our business logic using rest endpoints and reactjs is used as frontend user interface. Point here to be noted is unlike a traditional web application where java codes + web code (HTML + javascript + css) are residing in same project here spring boot and reactjs will be in different project. The main advantage of this type of setup is to have a loose coupling between your UI and business logic. Tomorrow if you feel to replace reactjs with anjularjs or vuejs you can very well do it without touching business logic.

Step 1 -
Create a spring boot application, click here for more details.

pom.xml


Monday, January 14, 2019

Hello World Parameterized Junit Test

In this article we will see how to build a Parameterized Junit Test.

Parameterized tests was introduced with JUnit 4. Basic concept of Parameterized tests is to run the same test over and over again using different inputs.

There are 4 steps involve in this -

  • Annotate the test class with @RunWith(Parameterized.class)
  • Create a public static method annotated with @Parameterized.Parameters that returns a Collection of Objects as test data set
  • Create a public constructor that takes input as a single data item from step 2
  • Create an instance variable for each item of test data


Lets see the example -

Created a maven project as shown below -


pom.xml

HelloWorld.java

HelloWorldTest.java