JWT Authentication and Authorization with Spring Boot and Vue.js: Complete Guide
Here's a brief explanation of the diagram's flow:
Actors:
- User (U): Represents the end user interacting with the application.
- LoginComponent (LC): The component in the frontend (Vue.js) where the user enters login credentials.
- AuthService (AS): A service in the frontend responsible for handling authentication, such as calling the backend to log in and storing the JWT token and user roles in local storage.
- LocalStorage (LS): A storage mechanism for saving the JWT token and user roles in the frontend.
- AuthController (AC): The backend controller responsible for handling authentication requests (e.g., login).
- JwtTokenUtil (JTU): A utility in the backend that generates and validates JWT tokens.
- UserController (UC): A backend controller that handles user-related routes.
- SecurityConfig (SC): A backend configuration that defines security rules and role-based access control.
Flow:
User Login:
- The User enters their credentials into the LoginComponent (LC).
- The LoginComponent sends the credentials to the AuthService (AS).
- AuthService calls the AuthController (AC) with the login request.
- The AuthController uses JwtTokenUtil (JTU) to generate a JWT token and returns it to AuthService.
- AuthService stores the JWT token and the user's roles in LocalStorage (LS).
User Accessing Protected Routes:
- The User navigates to protected routes, such as the user or admin dashboard.
- AuthService checks if the JWT token is valid by retrieving it from LocalStorage and validating it with JwtTokenUtil.
- If the token is valid, the User can access the routes.
Admin Role Validation:
- When accessing an AdminComponent, AuthService verifies that the user has the admin role by checking the stored roles in LocalStorage.
- If the role is valid, access is granted; otherwise, access is denied.
Backend Security:
- The UserController checks if the user is authenticated and authorized to access routes like
/user/home
. - For routes requiring admin privileges (e.g.,
/admin/home
), SecurityConfig ensures the user has the correct role (admin) before granting access.
User Login:
- The User enters their credentials into the LoginComponent (LC).
- The LoginComponent sends the credentials to the AuthService (AS).
- AuthService calls the AuthController (AC) with the login request.
- The AuthController uses JwtTokenUtil (JTU) to generate a JWT token and returns it to AuthService.
- AuthService stores the JWT token and the user's roles in LocalStorage (LS).
User Accessing Protected Routes:
- The User navigates to protected routes, such as the user or admin dashboard.
- AuthService checks if the JWT token is valid by retrieving it from LocalStorage and validating it with JwtTokenUtil.
- If the token is valid, the User can access the routes.
Admin Role Validation:
- When accessing an AdminComponent, AuthService verifies that the user has the admin role by checking the stored roles in LocalStorage.
- If the role is valid, access is granted; otherwise, access is denied.
Backend Security:
- The UserController checks if the user is authenticated and authorized to access routes like
/user/home
. - For routes requiring admin privileges (e.g.,
/admin/home
), SecurityConfig ensures the user has the correct role (admin) before granting access.
Complete Solution: JWT Authentication & Authorization
1. Backend: Spring Boot 3.x (JWT Authentication & Authorization)
Step 1: Create the Spring Boot Project
Create a Spring Boot project with the necessary dependencies.
In pom.xml
:
Step 2: Configure application.properties
Configure the H2 database for testing.
Step 3: Create JWT Utility Class
The JwtTokenUtil
class handles generating, validating, and extracting information from JWT tokens.
Step 4: Security Configuration with Role-Based Access
The SecurityConfig
class configures role-based authorization in Spring Security.
Step 5: Authentication Controller
The AuthController
generates a JWT token after successful login, including user roles.
Step 6: User Controller with Role-Based Access
Define the UserController
to restrict access to endpoints based on roles.
2. Frontend: Vue.js (JWT Authentication & Authorization)
Step 1: Create Vue.js Project
Start by creating a new Vue.js project using Vue CLI:
Install Axios and jwt-decode
:
Step 2: Auth Service
Create an auth.js
service to handle JWT login, logout, and storing roles.
Step 3: Vue Router with Role-Based Access
Configure the Vue Router to protect routes based on roles.
Step 4: Login Component
Create a login form in Login.vue
.
Step 5: Admin and User Components
Create the protected pages for Admin and User.
Admin.vue:
Home.vue (User Dashboard):
Final Notes
This is a fully functional, end-to-end JWT Authentication and Authorization solution with Spring Boot and Vue.js:
- Backend:
- JWT Authentication is implemented, and the token contains user roles.
- Role-based authorization restricts access to specific endpoints.
- Frontend:
- After logging in, the JWT is stored and decoded to access roles.
- Vue.js routes are protected based on user roles.
This example can be expanded to include database-backed user authentication, custom error handling, and better security practices (e.g., refresh tokens).