Skip to main content

Posts

Showing posts from 2019

Integrated/Kerberos authentication using Spring boot and SPNEGO API

In this tutorial we will learn how to use Spring boot and SPNEGO API to implement the kerberos or integrated authentication. Kerberos is developed by Massachusetts Institute of Technology (MIT) which is used to authenticate between trusted services using KDC tickets. For example email client, HR portal or employee portal in a corporate network where employee doesn't need to provide their user id & password to access these application in the same domain. Once they are logged in to their machine, they can access such services/application with kerberos authentication.
I am using MAC OS X to demonstrate the kerberos authentication. I have used the same machine to configure and run the application backed with kerberos authentication and my KDC database & application reside on the same machine.
Setting up kerberos  First of all we need to setup kerberos where we will configure the database, create user principals, create policies and keytabs. We need to create our application u…

Setting up kerberos in Mac OS X

Kerberos in MAC OS X Kerberos authentication allows the computers in same domain network to authenticate certain services with prompting the user for credentials. MAC OS X comes with Heimdal Kerberos which is an alternate implementation of the kerberos and uses LDAP as identity management database.
Here we are going to learn how to setup a kerberos on MAC OS X which we will configure latter in our application.
Installing Kerberos In MAC we can use Homebrew for installing any software package. Homebrew makes it very easy to install the kerberos by just executing a simple command as given below.
brew install krb5 Once installation is complete, we need to set the below export commands in user's profile which will make the kerberos utility commands and compiler available to execute from anywhere.
Open user's bash profile:
vi ~/.bash_profile Add below lines:
export PATH=/usr/local/opt/krb5/bin:$PATH export PATH=/usr/local/opt/krb5/sbin:$PATH export LDFLAGS="-L/usr/lo…

Efficient way to count characters or find duplicate characters in String

There are multiple ways to count the characters or find the duplicate characters in a String. Here we will see the two approaches for the same which may be useful in same or different scenarios.
Count or find duplicate characters using Hash Map With hash map we can keep the character count in the map against given character which is used as map key. Map uses hashcode to store the keys. To learn more on map storage, you can refer the link Why hashmap keys should be immutable.
public void countCharUsingMap(String str){ Map<Character, Integer> countMap = new HashMap<>(); for(Character ch:str.toCharArray()){ int count = countMap.get(ch)==null?1:countMap.get(ch)+1; countMap.put(ch, count); } countMap.forEach((k,v)->{ System.out.println("'"+k+"' : "+v+(v>1?"[DUP]":"")); }); } This code has stored the count for each character in Map, which we …

Spring Batch tutorial with example

Spring batch is used to create and process the batch jobs. It provides various features like logging, job statistics, transaction management, restarting jobs. It is very helpful in processing of large dataset but with finite volume of data.
In this tutorial we will learn how to create and execute the spring batch job. In our example we will create a job which will import all the words from a text file to database and then at last it will print the total number of words available in the database.
Below is the project structure.
Creating batch job Sample text file to importBelow is the contents of text file which we use for importing the words.
The list below gives you the 1000 most frequently used English words in alphabetical order. Once you've mastered the shorter vocabulary lists, this is the next step. It would take time to learn the entire list from scratch, but you are probably already familiar with some of these words. Feel free to copy this list into your online flashcard …

Multiple data source with Spring boot, batch and cloud task

Here we will see how we can configure different datasource for application and batch. By default, Spring batch stores the job details and execution details in database. If separate data source is not configured for spring batch then it will use the available data source in your application if configured and create batch related tables there. Which may be the unwanted burden on application database and we would like to configure separate database for spring batch.
To overcome this situation we will configure the different datasource for spring batch using in-memory database, since we don't want to store batch job details permanently.
Other thing is the configuration of  spring cloud task in case of multiple datasource and it must point to the same data source which is pointed by spring batch.
In below sections, we will se how to configure application, batch and cloud task related data sources.
Application Data Source Define the data source in application properties or yml configur…

Spring batch job with Spring cloud data flow server

Spring cloud data flow server is used to execute any batch job, command line task or streams as a microservice. Here we will learn how to register and execute a batch job with spring cloud data flow server. These jobs will be executed in separate JVM which gets created and destroyed on demand by spring cloud data flow server.
How to setup spring cloud data flow server Please refer below post on how to setup cloud data flow server.
https://www.thetechnojournals.com/2019/12/setting-up-spring-cloud-data-flow-server.html
Spring batch job Spring batch job is used for the batch processing or background job execution where we want to process the limited set of data. I will not show how to create batch jobs in this tutorial but we will learn what is required to register your batch job with spring cloud data flow server and how to execute it then.
Spring batch job registration with cloud data flow server To register a spring batch job we need to enable the task. Below are the steps required.
E…

Setting up Spring Cloud Data Flow Server

Spring cloud data flow server can be used to setup the pipeline of tasks or streams. Tasks and streams can be registered with data flow server and can be executed or monitored either using cloud data flow shell or data flow server UI console/ dashboard.
Setting up Data flow server Spring cloud data flow server can be setup using two ways, either use spring provided spring boot application or develop your own spring cloud data flow server as spring boot application.
Using Spring provided Data flow server application There are many ways to setup data flow server as given below.
Local MachineCloud FoundryKubernetes Please refer below documentation for more details for above installation types.
https://dataflow.spring.io/docs/installation/
Create Spring boot application as Data flow server Here I will show you how to create your own spring boot application as spring cloud data flow server which you can run anywhere locally or in cloud. It gives you more freedom with application to setup th…

H2 database as in-memory database with DB access console in Spring boot

In memory database are those who uses computer's RAM for storage and remains available in single execution. Once you restart them, it will loose all the data and you will see the fresh database. They are useful during development or unit test execution.
With Spring boot application it is very easy to create in-memory database by declaring few properties only and adding the spring boot starter for in-memory database, rest will be done by spring boot itself. We will use H2 database with Spring boot but it supports other databases also like,  HSQLDB and Derby.
Configure H2 database as in-memory database Maven dependencies Add below dependency for H2 database.
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> application.properties changes Actually, you don't need to configure any properties. Spring boot does it for you but if you want to c…

Annotation processor to generate DTO classes from Entity class

Annotation Processor Annotation processors can be used to generate the classes at compile time basis on specified annotations. So we can define annotations, we can use them on classes and then we can process them using annotation processor.
Here we will see how we can use Annotation processor in Java to generate the classes at runtime. We will try to generate the DTO (Data transfer object) classes from entity classes. There are situation when a developer has to write DTO classes for the Entity class with almost same fields, so data can be transferred from one layer to another layer like UI to backend and backend to UI. We can use entity objects also to transfer between UI and backed but it will create more problem instead of solving as entity objects live in transaction context and reading or updating them in non-transactional context may change their state which may not be as expected. I just took the example of UI and backend but there can be many other scenario when it is required…

Microservices - Config management using Spring cloud bus and Rabbit MQ

In this tutorial we will learn how to add Rabbit MQ capabilities to our Spring cloud config server so any changes to configurations can be pushed to all connected applications during runtime. We need such kind of behaviour when we need to refresh the properties without restarting our application. Below is the overall architecture for this complete setup.
Spring Cloud Config Server Spring cloud config server is used to setup the distributed configuration using GIT or local file system where we can keep our configuration files and serve as them from Spring cloud config server. Client application just has to connect with config server by providing their application and profile name for specific configuration. Please refer below link where I have explained more about cloud config and how to code it.
https://www.thetechnojournals.com/2019/10/spring-cloud-config.html
Install Rabbit MQ Please refer below link on how to install rabbit-mq and virtual host verification.
https://www.thetechnojour…