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

Leave a Reply

Your email address will not be published. Required fields are marked *