Spring Boot — JWT Token based resource authentication & JavaMailSender
The idea behind that sending JWT token to the developer email while working in the development phase.
Problem
In the current trend of running with the crowd, I have experienced that when any secure microservices module is in the development phase the developer’s pain is hitting a controller that gets generate the JWT token, Why don’t set the MAX_EXPIRE 24hrs or 8hrs and send it to the developer’s email address.
Implementation
Here is explanation of the implementation. See the gradle.build for required dependencies from below link.
https://github.com/saurabhshcs/bootifyjpajwt/blob/main/build.gradle
JWTService
We need to create an utility class or service that will be responsible to following functionalities-
1- generateToken
2- validateToken
3- extractClaim
4- extractExpiration
5- extractUserName
6- extractAllClaims
Please refer following link for looking of the implementation of these methods.
Request Filter — that validate every request
Filter that is used for inspecting every request of the user and extract the userName from the JWT token and validate that JWT token
{
“jwt”: “eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb28iLCJleHAiOjE2MDYzOTU0ODMsImlhdCI6MTYwNjM1OTQ4M30.bmWzebUtCos1a-eTQK7c28XIMos46_qjqvL5YwUSLCk”
}
https://github.com/saurabhshcs/bootifyjpajwt/blob/main/src/main/java/com/techsharezone/bootifyjpajwt/filter/JwtRequestFilter.java
AuthenticateController
This controller is used to generateToken and send that token to the developer’s email address. (as of now hard coded but we can pick it from IDE configurations)