Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Springboot 3 with JWT Authentication & Authorization in 2025
Rating: 4.2 out of 5(151 ratings)
19,324 students

Springboot 3 with JWT Authentication & Authorization in 2025

UPDATED-2025 - Learn to secure your SpringBoot based RESTful API's using Role Based JWT Authentication & Authorization
Created byRanjan Pandey
Last updated 1/2025
English
English [Auto],

What you'll learn

  • You will learn basics of security like Authentication and Authorization
  • You will learn everything about JWT(JSON Web Token)
  • You will learn to Implement JWT with SpringBoot and Spring Security
  • You will learn to Develop CRUD API and secure it with JWT
  • You will learn to Implement Role Based Authentication & Authorization with JWT & Spring Security
  • You will learn to setup a SpringBoot project from scratch
  • You will learn to create API's which will talk to Database using Spring Data JPA
  • You will learn to protect your API's based on user role
  • You will get the complete source code

Course content

3 sections47 lectures3h 4m total length
  • What is JWT and How does it work to serve Token based Authentication and Author5:41

    Explore how json web token enables stateless authentication between client and server by generating a short-lived token with a secret, embedding user data, and authorizing access.

  • How does JWT token look like and its different parts3:30

    Learn how JSON Web Token enables secure, stateless authentication and authorization between client and server, and how header, payload, and signature parts combine with base64 encoding to form the token.

  • Understanding the JWT Workflow3:25

    Explore the complete jwt workflow in a Spring Boot 3 app, from the authentication filter to the jwt service, user validation, and authorization.

  • Software Download and Installation1:45

    Install JDK 17 using Adoptme, pick the Windows 64 MSI, install IntelliJ Community Edition, and set up Postman to test APIs.

  • Generating the starter springboot 3.3.5 project2:13

    Generate a starter Spring Boot 3.3.5 project on start.spring.io with Maven, Java 17, and dependencies like Spring Web, Spring Security for JWT authentication, Lombok, and Spring Data JPA.

  • Intellij Project Settings2:40

    Explore the IntelliJ editor setup for Spring Boot 3 by importing the project, selecting JDK 17, and adjusting the editor font size to 18, including theme preferences.

  • Adding JWT dependencies2:40

    Add three jwt dependencies to the pom.xml (jwt api, jwt impl, jackson) from io.jsonwebtoken, refresh maven, ensure java 17 in the project structure, and adjust IntelliJ font if desired.

  • Adding Different User Roles1:34

    Create the project structure by adding packages and an enum for user roles, such as admin, user, and moderator, to configure JWT-based authorization for role-specific activities.

  • Creating Role Entity1:43

    Create the role entity with Spring Data JPA, map the roles table with an auto-generated id, and use Lombok for getters and setters.

  • Creating User Entity3:00

    Create a user entity with Lombok and map a many-to-many relationship to roles via the users_to_role join table, detailing fields and front-end date input handling.

  • Adding profile based application properties files1:45

    Configure spring boot profiles by creating profile-specific properties files like application-local.properties and application-prod.properties, and enable them via spring active profiles to run the app with local or production settings.

  • Adding H2 file based Database configuration2:55

    Configure the local profile to use a file-based H2 database, enable the H2 console, and apply ddl auto; use environment variables for the JWT secret and expiration.

  • Creating Role Repository with required custom queries4:12

    Create a role repository interface using Spring Data JPA to perform CRUD operations and define a custom query to find a role by name, with enum values stored as strings.

  • Creating User Repository with required custom queries1:20

    Create a user repository interface in Springboot 3 to interact with the users table, including custom queries to find by email and check existence by email, paving the service layer.

  • Adding logic for UserDetailsImpl class for mapping the User Entity and Role6:36

    Map the user entity and its roles to spring security by implementing userdetailsimpl, including extra fields like phone, nationality, date of birth, and gender, and building authorities for jwt authentication.

  • Adding logic for UserServiceImpl to load user and save user2:53

    Develop user service logic to load users by email from repository, save new users with encoded passwords, and build user details for authentication.

  • Service for generating validating token and extracting claims6:21

    Create and manage JWT tokens in Spring Boot by generating access tokens with subject and claims, validating tokens, extracting claims, and supporting bearer authentication for secure user interactions.

  • Implementing JWT Authentication Filter6:51

    Implement a JWT authentication filter that intercepts requests, extracts and validates the bearer token from the authorization header, and sets the user in the security context for protected endpoints.

  • Implementing and understanding Security Config bean4:06

    Implement a security config bean to connect jwt with Spring Security, configure password encoder and authentication provider, enable web and method security, and define the security filter chain.

  • Exception handling for all security errors1:55

    Implement a dedicated exception handling class and a JWT authentication entry point to capture, log, and forward security errors, then wire this error handler into the security configuration.

  • Understanding Security Filter Chaining configuration2:34

    Configure a security filter chain in Springboot 3, disable CSRF, enable exception handling, enforce stateless sessions, and apply a JWT filter before the username-password authentication filter for protected URLs.

  • Handling Cors Configuration in the application1:24

    Create a cors configuration class to enable cross-origin requests and prevent cors issues by allowing all origins and all http methods for any url.

  • Creating Signup Request DTO3:34

    Create a signup request dto with validation annotations for email, first name, last name, and goals; import spring validation and add the dependency to pom.xml to enable validation.

  • Creating LoginRequest and Response DTO and Message DTO2:43

    Create login request dto with email and password and a jwt response dto including token, user id, token type, first name, email, roles, and a message response dto.

  • Implementing Signup and Registration functionality in Auth Controller7:42

    Create an auth controller in spring boot 3, integrate a JWT service to generate and validate tokens, and set up default roles including user, admin, moderator for signup and registration.

  • Understanding Auth Controller2:48

    Explore the auth controller, including authentication manager, user and role repositories, password encoding, and JWT issuance, plus registration flow with email checks and default roles.

  • Startup command to insert roles into database2:28

    Create a startup command using a command line runner to seed the database with admin, moderator, and user roles, ensuring roles exist before saving in a Spring Boot application.

  • Solving circular dependency2:42

    Enable spring.main.allow-circular-references to resolve a circular dependency in a spring boot app, bootstrap with admin and moderator roles, create tables, and test sign in and sign up endpoints.

  • Testing the application4:22

    Practice testing a Spring Boot app with Postman, creating a user via a no-auth endpoint, exploring sign-up and sign-in flows, and debugging 401 unauthorized on whitelisted endpoints.

  • Updating Security Config rules and testing again4:44

    Update security config: disable CSRF, define public URL patterns, enforce stateless JWT authentication, and test role-based access with tokens (admin, user, moderator) in Spring Boot 3.

  • Implement Test Controller to test Authentication and Authorization1:59

    Create a test controller for authentication and authorization, configure role-based access for api v1 endpoints (all, user, moderator, admin), enforce authentication, and run the app to test.

  • Testing different roles with API endpoints security3:24

    Test api endpoint security by applying pre-authorized rules to admin, moderator, and user roles, using jwt authentication to access endpoints, and observe public content, 403 errors, and role-based access.

  • Get LoggedIn user information4:25

    Learn how to retrieve the currently logged-in user via a simple util that reads authentication from the security context, enabling easy access to user details in Spring Boot with JWT.

  • Defining ErrorDTO and BusinessException in Backend API3:00

    Define a business exception and error dto, update the controller to return 201 with a new response entity, and prepare a centralized error handler for the backend.

  • Implementing Global Exception Handler2:04

    Define a global exception handler to centralize error handling in Spring Boot 3. Replace error models with error dto and return a list of field validation errors and business exceptions.

  • Mapping the Login Response to right fields2:01

    Map the login response to the correct fields in the right order: JWT token, id, first name, last name, email, and role, and implement constructors for streamlined error handling.

  • Source code

Requirements

  • Basic knowledge of Java
  • Basic knowledge SpringBoot

Description

This course has been updated in 2025 as per new Spring Security Features and JWT Specification to implement Role Based Authentication & Authorization of any SpringBoot based Application.

  • Develop CRUD API and secure it with JWT

JWT(Json web token) is a standard for securing API's in a Microservices architecture, using this standard we can secure API's built in any technology like Nodejs, Python, .NET etc but in this course we will building API's using Java and SpringBoot and than we will be securing them using JWT and Spring security, we will also use all the industry best practices and standards along the way.

We will start with learning concepts like:

  • What is Authentication

  • What is Authorization

  • Importance of securing RESTful API's

  • What is JWT(Json Web Token)

  • Various components and terminology associated with JWT

  • Workflow and a diagramatic use case of using JWT

Than we will move on to the actual handson and implementation of JWT in our Springboot project by following below steps:

  • Create springboot project from scratch

  • Adding required maven dependencies

  • Setup project in Github and follow continous integration process

  • Setting up database and establishing connection

  • Create our Hibernate entity classes

  • Create our controller class

  • Create the first API

  • Start configuring JWT and Spring security

  • Create security related classes like configuartion, filters, service

  • Making the secret and expiration time configurable

  • Configuring the allowed and not allowed endpoints

  • Creating user registration and login endpoints

  • Creating Role based classes

  • Create multiple roles

  • Restrict users to functionalities based on Roles

  • Setting up github to push code with token

  • Creating a JWT token

  • Using the JWT token to call a secured API

  • Using Postman to test our API's

  • Mechanism to check if a particular JWT token is valid or not

  • Extracting user information and role information from JWT token

  • Develop CRUD API and secure it with JWT

  • Complete source code

Who this course is for:

  • Anyone who wants to learn about securing API's at an industry grade standard