This project demonstrates a Spring Boot application with secure authentication and authorization using JSON Web Tokens (JWT), Role-Based Access Control (RBAC), MySQL database integration, and the jjwt library. Users can register, log in, generate tokens, and access protected resources based on their assigned roles.
- JWT Authentication: Secure user login with JWT for stateless authentication.
- RBAC Implementation: Control access to resources based on user roles.
- MySQL Integration: Stores user data and roles in a persistent database.
- jjwt Library: Handles JWT generation, validation, and parsing.
- Protected Routes: Restricts access to specific endpoints based on roles.
- Clear Architecture: Well-structured code for maintainability.
Users can create accounts with usernames and passwords. User roles are assigned during registration.
Users provide credentials (username and password) for authentication. Upon successful login, a JWT token is generated and returned.
Users include the JWT token in the Authorization header of subsequent requests. The server validates the token and grants access to authorized resources.
Specific endpoints are secured for users with designated roles (e.g., /dashboard for admins). Unauthorized requests result in access denial responses.
- Java 11+
- Maven or Gradle
- MySQL database server
- Running the Application: ♀️
Set up a MySQL database with necessary tables (user, role, etc.).
Configure database connection details in application.properties.
Run the application using: Bash
mvn spring-boot:run
Use code with caution.
The server will start on port 6000.
Use a POST request to http://localhost:6000/login with JSON credentials in the request body:
JSON
{
"username": "your_username",
"password": "your_password"
}
Note
Use code with caution.
{
"token": "your_jwt_token"
}
Use code with caution.
Access the root endpoint (/) with any HTTP method (GET, POST, etc.). This endpoint should be accessible to everyone (Role: ROLE_USER, ROLE_ADMIN).
Access the /dashboard endpoint with a GET request.
Include the received JWT token in the Authorization header:
Authorization: Bearer your_jwt_token
Only users with the "ADMIN" role will be able to access this endpoint.
- Spring Boot
- Spring Security
- JWT (jjwt library)
- MySQL
[Optional] Additional libraries for specific functionalities
We welcome contributions to improve this project. Feel free to fork the repository, make changes, and create pull requests.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Consider using environment variables for sensitive information like database credentials. Implement robust error handling and security best practices for production deployments.