This is a Spring Boot backend project implementing secure API authentication using JWT (JSON Web Tokens) and Bcrypt for password hashing. The project connects to a MySQL database and provides basic user registration, login, and CRUD operations for Student
resources.
- JWT-based Authentication:
- Secure login endpoint for token generation.
- Token validation and stateless session management.
- Password Hashing:
- Secure password storage using Bcrypt.
- CSRF Protection:
- CSRF token endpoint for additional security.
Here are some planned enhancements for this project:
- Role-Based Authorization:
- Implement
@PreAuthorize
or similar mechanisms to restrict access based on roles (e.g., Admin, User, Student). - Add role-based endpoints for managing sensitive operations.
- Implement
- Refresh Tokens:
- Add support for refresh tokens to allow seamless JWT renewal.
- Pagination for Student List:
- Implement pagination for the
/students
endpoint to handle large datasets efficiently.
- Implement pagination for the
- Improved Error Handling:
- Introduce global exception handling for better API responses.
- Dockerization:
- Create a
Dockerfile
to containerize the application for easy deployment.
- Create a
- Front-End Integration:
- Build a front-end client using React or Angular for interacting with the API.
- API Documentation:
- Use Swagger/OpenAPI for comprehensive API documentation.
- Spring Boot: Backend framework
- Spring Security: Authentication & Authorization
- JWT (JSON Web Tokens): Token-based authentication
- Bcrypt: Secure password hashing
- MySQL: Relational database
- Hibernate/JPA: ORM for database operations
HTTP Method | Endpoint | Description |
---|---|---|
GET |
/ |
Welcome message with session ID. |
GET |
/students |
Returns a list of all students. |
POST |
/students |
Adds a new student to the list. |
GET |
/csrf-token |
Returns a CSRF token. |
POST |
/register |
Registers a new user. |
POST |
/login |
Authenticates the user and returns a JWT token. |
- Java 21+
- Maven 3.8+
- MySQL Database
- IDE (e.g., IntelliJ IDEA)
-
Clone the repository:
git clone https://github.com/athrocks/spring-jwt-auth-API.git cd spring-jwt-auth-API
-
Configure the database:
Update the application.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
- Build the project:
./mvnw clean install
- Run the project:
./mvnw spring-boot:run
- Access the API at http://localhost:8080.
CREATE TABLE users(
id integer primary key,
username varchar(50),
password varchar(50)
);
Use Postman or cURL to test endpoints.