Refresh Token : Create JWT token with No Expiration Spring Boot Example

1+

As a backend developer, I am facing JWT token expired exception (JWTExpiredException) at functional testing 🤯. I think many of us facing same problem in the daily life coding 🤬. Want to come out with this problem. Then this article is for you.🤩

Refresh Token : Create JWT token with No Expiration Spring Boot Example
Create JWT token with No Expiration Spring Boot Example

Let’s dive in the code without wasting your time.⌚ You can download the code from Git below.✨

Create JWT token with No Expiration Spring Boot

First of all let’s move to the application.properties file


We are using simple Spring Boot JWT web token example and as you can see that the jwt.expirationDateInMs=50000 define the normal token expiration and jwt.refreshExpirationDateInMs=9000000 define the refresh token expiration.

Now its time to configure the refresh token with our service class (JWTUtil)


Now we have to implement the filter for JWT authentication. In this filter named CustomJwtAuthenticationFilter, If JWT token expired during the testing then we have to set isRefreshToken set to true.

Create custom Authorization Filter

When the token is expired then we have to get claims from the ExpiredJWTException and store it in HttpRequest. These claims would be use to create refresh JWT token. And we have to set Security context by create the UsernamePasswordAuthenticationToken with null values.


Now we can move to the create the refresh token in AuthenticationController.


That’s it. Now you can check the demo of this example.

Demo Screenshot

Register the user

Register the user
Register the user

Authenticate the URL

Authenticate the URL
Authenticate the URL

Access the Admin section with normal token

Access the Admin section with normal token
Access the Admin section with normal token

Create Refresh Token

Create Refresh Token
Create Refresh Token

Download the source code

1+