Spring Boot Refresh Token using JWT example - Expire and Renew JWT Token

Overview

Spring Boot Refresh Token with JWT example

Build JWT Refresh Token in the Java Spring Boot Application. You can know how to expire the JWT, then renew the Access Token with Refresh Token.

The instruction can be found at: Spring Boot Refresh Token with JWT example

User Registration, User Login and Authorization process.

The diagram shows flow of how we implement User Registration, User Login and Authorization process.

spring-boot-spring-security-jwt-authentication-flow

And this is for Refresh Token:

spring-boot-refresh-token-jwt-example-flow

Spring Boot Server Architecture with Spring Security

You can have an overview of our Spring Boot Server with the diagram below:

spring-boot-jwt-authentication-spring-security-architecture

Related Posts:

Spring Boot, Spring Security, MySQL: JWT Authentication & Authorization example

For PostgreSQL

For MongoDB

Fullstack Authentication

Spring Boot + Vue.js JWT Authentication

Spring Boot + Angular 8 JWT Authentication

Spring Boot + Angular 10 JWT Authentication

Spring Boot + Angular 11 JWT Authentication

Spring Boot + React JWT Authentication

Fullstack CRUD App

Vue.js + Spring Boot + MySQL/PostgreSQL example

Angular 8 + Spring Boot + MySQL example

Angular 8 + Spring Boot + PostgreSQL example

Angular 10 + Spring Boot + MySQL example

Angular 10 + Spring Boot + PostgreSQL example

Angular 11 + Spring Boot + MySQL example

Angular 11 + Spring Boot + PostgreSQL example

React + Spring Boot + MySQL example

React + Spring Boot + PostgreSQL example

React + Spring Boot + MongoDB example

Run both Back-end & Front-end in one place:

Integrate Angular with Spring Boot Rest API

Integrate React.js with Spring Boot Rest API

Integrate Vue.js with Spring Boot Rest API

More Practice:

Spring Boot File upload example with Multipart File

Exception handling: @RestControllerAdvice example in Spring Boot

Spring Boot Repository Unit Test with @DataJpaTest

Deploy Spring Boot App on AWS – Elastic Beanstalk

Secure Spring Boot App with Spring Security & JWT Authentication

Dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>

<dependency>
	<groupId>io.jsonwebtoken</groupId>
	<artifactId>jjwt</artifactId>
	<version>0.9.1</version>
</dependency>

Configure Spring Datasource, JPA, App properties

Open src/main/resources/application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username= root
spring.datasource.password= 123456

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update

# App Properties
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 3600000
bezkoder.app.jwtRefreshExpirationMs= 86400000

Run Spring Boot application

mvn spring-boot:run

Run following SQL insert statements

INSERT INTO roles(name) VALUES('ROLE_USER');
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
You might also like...

This project contains a full example of an application developed using Spring Boot and GraphQL within the Java.

Spring boot GraphQL Example This project contains a full example of an application developed using GraphQL within the Java. The project includes a com

Jul 20, 2022

Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

Dec 28, 2022

Zitadel.ch Example Project with Spring Boot and Spring Security

Zitadel.ch Example Project with Spring Boot and Spring Security

Zitadel Example Project with Spring Boot and Spring Security This example contains two Spring Boot Apps (app and api) which use the Zitadel IdP as Ope

May 3, 2022

This is an example of how conditional events can be triggered in Camunda using a simple spring boot project

This is an example of how conditional events can be triggered in Camunda using a simple spring boot project

Camunda Conditional Events Example This example is a Spring Boot Application using Camunda. In this example i'll show a variety of ways that BPMN's Co

Sep 30, 2021

Project Basics Example using Spring Boot 3.0

Base Project Spring 3.0 Exaples of the acess - API localhost Method create - POST Content-Type =

May 9, 2022

Spring boot microservice example with Eureka Server + Eureka Client + Spring Cloud API Gateway + OAuth2.0 + Circuit Breaker + Resilience4J + FeignClient + RestTemplate

Spring boot microservice example with Eureka Server + Eureka Client + Spring Cloud API Gateway + OAuth2.0 + Circuit Breaker + Resilience4J + FeignClient + RestTemplate

Spring boot microservice example Spring boot microservice example with Eureka Server + Eureka Client + Spring Cloud API Gateway + OAuth2.0 + Circuit B

Dec 29, 2022

Spring JPA @Query for custom query in Spring Boot example

Spring JPA @Query example (Custom query) in Spring Boot Use Spring JPA @Query for custom query in Spring Boot example: Way to use JPQL (Java Persisten

Dec 3, 2022

Spring JPA Native Query example in Spring Boot

Spring JPA Native Query example in Spring Boot

Nov 30, 2022

Implementing JWT authentication with spring boot.

Jwt-SpringBoot Implementing JWT authentication with spring boot. Normally you would create an endpoint to create the credentials(token), then this tok

May 7, 2022
Comments
  • How about this way

    How about this way

    Hi I implemented a new way of refresh tokens and I don't know that it is secure or optimized. this is how it works:

    1- User logs in or sign up 2- Refresh token with expiration and Access token with no expiration created and sends to client 3- These created tokens, stored in database like this:

    table: tokens user_id | refresh_token | access_token 1 | some_token | some_token

    4- When clients requests for an endpoint, it should send refresh_token and access_token in the header

    5- Server checks if the refresh_token expired or not and also checks both refresh_token and access_token with stored ones if they are equal or not

    6- If everything was OK, server generates new access_token and updates the table and sends back to client

    By this way, access_tokens are used once. If client request for an endpoint and it was successful, server sends back a new access_token and the client for every next request alongside refresh_token, should send this new access token

    opened by DarkDeveloper-arch 2
  • Please upgrade to Spring Boot 3.0.0 with Spring Security 6.x

    Please upgrade to Spring Boot 3.0.0 with Spring Security 6.x

    Please upgrade to Spring Boot 3.0.0 with Spring Security 6.x

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    

    cannot work with Spring Boot 3.0.0 . See https://stackoverflow.com/questions/74667235/spring-security-6-when-upgrade-to-spring-boot-3-org-springframework-security-a

    and

    https://stackoverflow.com/questions/74666596/how-to-fix-error-of-websecurityconfigureradapter-when-upgrade-to-spring-boot-3-0

    Thank you very much!

    opened by donhuvy 3
Owner
Programming languages & technique for web/mobile/cross-platform application development
null
Spring Boot Security Login example with JWT and H2 example

Spring Boot Security Login example with JWT and H2 example Appropriate Flow for User Login and Registration with JWT and HttpOnly Cookie Spring Boot R

null 50 Dec 21, 2022
Spring Boot & MongoDB Login and Registration example with JWT, Spring Security, Spring Data MongoDB

Spring Boot Login and Registration example with MongoDB Build a Spring Boot Auth with HttpOnly Cookie, JWT, Spring Security and Spring Data MongoDB. Y

null 15 Dec 30, 2022
Spring Boot JWT Authentication example with Spring Security & Spring Data JPA

Spring Boot JWT Authentication example with Spring Security & Spring Data JPA

null 1 Jan 26, 2022
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的线上博客项目/企业大型商城系统/前后端分离实践项目等,摆脱各种 hello world 入门案例的束缚,真正的掌握 Spring Boot 开发。

Spring Boot Projects 该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的前

十三 4.5k Dec 30, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

null 7 Dec 20, 2022
ReactJS, Spring Boot JWT Authentication Example

springboot-reactjs-jwt-authentication ReactJS - SpringBoot - JWT - Flow Local setup Step 1: Download or clone the source code from GitHub to a local m

Sibin Rasiya 16 Dec 2, 2022
This module explains about the example of Spring MVC + Database Integration with MySQL using Hibernate ORM with practical coding example and required JAR dependencies

SpringMVC-Database-Integration This module explains about the example of Spring MVC + Database Integration with MySQL using Hibernate ORM with practic

GowthamRaj K 3 Nov 2, 2021
Spring Boot REST API authentication best practices using JWT

Spring Boot REST API authentication best practices using JWT Token based API authentication with Spring Security and JWT (JSON web Token) Overview Thi

Prafful Lachhwani 34 Dec 22, 2022
The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker.

gke-springboot-sampleapp ?? The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker. Usage To be

KYEONGMIN CHO 1 Feb 1, 2022