SpringBoot Token Based Authentication Example – JWT + MySQL + Spring JPA + RestAPIs [DEBUG]

Опубликовано: 12 Октябрь 2024
на канале: loizenai
1,209
14

#springboot #jwt #authentication #restapi
Spring Boot Security JWT Token Based Authentication Example – MySQL + Spring JPA + RestAPIs [DEBUG]

https://loizenai.com/spring-boot-secu...

JSON Web Token defines a compact and self-contained way for securely transmitting information as a JSON object. “How to build SpringBoot Security JWT Authentication?” is one of the most common questions for Java development world. So in the tutorial, I will introduce how to build it with clearly architecture and coding examples.

I. Spring Security – JWT Authentication Architecture

When a HTTP request comes (from a browser, a web service client, an HttpInvoker or an AJAX application – Spring doesn’t care), it will go through a chain of filters for authentication and authorization purposes.
So, it is also true for a User Authentication request, that filter chain will be applied until relevant Authentication Filter is found.

II. Filter the Request
– Login/SignUp: RestAPI with non-protected APIs - authenticate Login Request with AuthenticationManager, if error occurs, handle AuthenticationException with AuthenticationEntryPoint.
– With protected Resources:
jwt token is null/invalid - if Authenticated Error occurs, handle AuthenticationException with AuthenticationEntryPoint.
jwt token is valid - from token, get User information, then create AuthenticationToken.

V. Video Structure

1. Overview Debug Spring Boot JWT Authentication

0:00 - Overview Debug Spring Boot JWT Authentication

2. Overview SpringBoot Article

0:34 - Review Overall SpringBoot JWT Authenticated Article
0:47 - Start Download SpringBoot JWT Authentication Sourcecode

3. SpringBoot JWT SignUp Debug

1:09 - Run SpringBoot with Debug mode
1:13 - Review SpringBoot JWT Project Structure
1:21 - pom.xml file
1:27 - application.properties file
1:31 - roles.sql file
1:37 - UserPrinciple.java file
1:43 - UserDetailsServiceImpl.java file
1:47 - WebSecurityConfig.java file
1:55 - JwtProvider.java file
2:04 -JwtAuthTokenFilter.java file
2:06 - JwtAuthEntryPoint.java file
2:11 - UserRepository.java file
2:16 - RoleRepository.java file
2:19 - User.java file
2:23 - RoleName.java file
2:25 - Role.java file
2:30 - LoginForm.java file
2:34 - SignUpForm.java file
2:38 - JwtResponse.java file
2:42 - ResponseMessage.java file
2:46 - AuthRestAPIs.java file
2:50 - TestRestAPIs.java file

4. SpringBoot JWT SignIn Debug

SIGN UP

2:56 - review MySQL tables
3:24 - Start Debug SignUp a User (JACK with USER Role) to SpringBoot JWT Token Based Authentication
3:57 - Start Debug SignUp a User (ADAM with Roles: PM & USER) to SpringBoot JWT Token Based Authentication
4:42 - Start Debug SignUp a User (Thomas with Roles: ADMIN) to SpringBoot JWT Authentication

SIGN IN and Access PROJECTE Resource

5:27 - SignIn with Jack has User role
7:17 - Successfully SignIn with Jack user role and returned a JWT Token based for access protected resource
7:30 - Debug Jack accesses protected resources with USER role. Jack can access resources for USER role but NOT access resources for PM and ADMIN roles.

10:41 - SignIn with Adam having PM and USER roles
11:31 - Successfully SignIn with Adam (Roles: USER & PM) and returned a JWT Token based for access protected resource
11:48 - Debug Adam accesses protected resources with USER & PM role. Jack can access resources for USER & PM roles but NOT access resources for ADMIN role.

12:57 - SignIn with Thomas having ADMIN role
13:32 - Successfully SignIn with Thomas (role: ADMIN) and returned a JWT Token based for access protected resource
13:41 - Debug Thomas accesses protected resources with ADMIN role. Thomas can access all resources.

14:20 - Try to Access a resource with invalid JWT Token

VI. Related Post

1. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete
https://loizenai.com/angular-crud-app...

2. SpringBoot Upload Download Multiple Files Examples with Thymeleaf
https://loizenai.com/springboot-uploa...

3. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database)
https://loizenai.com/build-springboot...


The Article: Spring Boot Security JWT Token Based Authentication Example – MySQL + Spring JPA + RestAPIs [DEBUG]
https://loizenai.com/spring-boot-secu...