how to create json web token (jwt) using java

In this tutorial we will see how we gonna create json web token using java

What is JWT ?

JSON Web Token is string in encrypted format. After the decryption of the JWT String will have json information.

JWT is generally used for session validations and to pass user information to client side in secure way. every time when a user access the pages server will send a JWT token. which is used JWT token is used for user authentication at server side.

what is the JWT Structure

 

we can create JWT token using different programming languages. it is independent of programming language.

 

How to Create JWT token in java.

 

Create a simple maven project. add below dependency

Below is the Code to generate the JWT token

in above code we are creating the JWT token for Map object with subject as json web token. generated jwt token is using SignatureAlgorithm.HS512.  we can use the different algorithms by referring api.

below are all available algorithms

Created token will be looks like below

 

For Decoding the JWT Token below is the code

Jwstoken  is the token generated and sent to client.

below is the complete class for encoding and decoding of JWT

 

 

Note: For decoding JWT Token setSigningKey should be same as the Key used for encoding

 

Happy Coding

creating-entity-classes-from-database-schema

In this tutorial will see how to create the entity classes from db schema.

below are the required software’s to be installed in you local machine.

steps to download and install Hibernate Tools

After installation of hibernate tools follow below steps for creating entity class

go to perspective and select database development as below screen shot

 

  • Click on new profile

  • Selecting the database type from Popup

  • Next select the drivers in

  • Eclipse will show predefined drivers version, If your mysql drivers in list then select the respective drivers configuration and upload the respective drivers jar from JAR List tab as below. if your mysql version is not in list then upload downloaded drivers jar from JAR List tab

  • Click in Add Jars/ZIP button to browse the drivers jar file
  • After uploading the drivers jar you can see as above screen shot
  • Next add the DB configuration Setting in Properties Tab

  • in my case localhost is my database ip address
  • test is my database name
  • root is user name
  • for drivers class click browse button as shown below

  • Click blue dots button to add drivers class file

  • Select drivers class as com.mysql.jdbc.Driver, click on ok button
  • you will see the final configuration overview to finish setup as below

  • Provide the password and confirm the database and user name and click on test connection you should see below alert

  • Click on Ok button on popup and click on Finish button
  • you will see the connection details and database schema  in left side Data Source Explorer

Now Step 2 Creating JPA project 

  • Change Perspective to JPA 

  • Create new JPA Project as below steps

  • Select JPA Project under JPA folder
  • Use settings as above screen shot
  • Select the hibernate (JPA 2.1)

  • select connection which we created in step 1

  • Click on Finish button you will see project name in left side Project Explorer

  • Next creating Entities using jpa project
  • Right click on JPA project to see below options

  • Click on generate entities from tables

  • enter the package name to where classes need to be create and click on finish

  • Respective Entity classes created with respective the tables in database test
  • copy paste the entity classes into you project folder

 

 

Happy programming