JWT + Spring Boot

Published On: 27 December 2022.By .
  • General

In this blog, we will discuss how to add the JWT(JSON Web Token) Authentication with Spring Boot to secure our Rest API’s.

What is JWT ?

When two systems exchange data, you can use a JSON Web Token to identify your user without having to send private credentials on every request.

Flow of Request

JWTUserDetailsService :

It implements the UserDetailsService interface of  Spring Security and overrides the loadUserByUsername for fetching user details from the database. The Spring Security Authentication Manager calls this method for getting the user details from the database when authenticating the user details provided by the user.

JWTTokenUtil :

It is responsible for creating and validating the token.

JWTRequestFilter :

When the client sends the request, it first comes to it. It checks whether it has valid JWT token or not. If it has, then it sets the authentication in the context so the current user is authenticated.

AuthenticationController :

It has the Login and Register API. In this we call the JWT Token Util to generate the token.

WebSecurityConfig :

In this we can customize the web security and Http security.

In this way we can use the JWT with the Spring Boot and secure our API’s from the unauthorized access.

Related content

That’s all for this blog